java 我如何使用 Mockito 来模拟 Spring ApplicationContext 的 getBean 方法以使用 TestNG 编写单元测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43526479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I mock Spring ApplicationContext's getBean method using Mockito for writing unit tests with TestNG?
提问by yaswanth
I am writing unit tests for the following class
我正在为以下课程编写单元测试
Class to Be Tested:
要测试的类:
public class RandomManager {
@Autowired
private ApplicationContext context;
@Autowired
private ClassA objectA;
public void methodToBeTested() {
objectA.methodToBeVerified(context.getBean(Random.class,"Yaswanth","Yaswanth"));
}
}
Below is the test class:
下面是测试类:
public class RandomManagerTest {
@Mock
private ClassA objectA;
@Mock
private ApplicationContext context;
@InjectMocks
private RandomManager randomManager;
@BeforeTest
public void before() {
MockitoAnnotations.initMocks(this);
doReturn(any(Random.class)).when(context)
.getBean(any(Class.class), any(), any());
}
@Test
public void methodToBeTestedTest() {
Random randomObject = new RandomObject("Yaswanth", "Yaswanth");
randomManager.methodToBeTested();
verify(objectA).methodToBeVerified(randomObject);
}
}
The above code fails in the before method when I am trying to stub the applicationContext mock. I get the following error.
当我尝试存根 applicationContext 模拟时,上面的代码在 before 方法中失败。我收到以下错误。
You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains("foo"))
This message may appear after an NullPointerException if the last matcher is returning an object like any() but the stubbed method signature expect a primitive argument, in this case, use primitive alternatives. when(mock.get(any())); // bad use, will raise NPE when(mock.get(anyInt())); // correct usage use
Also, this error might show up because you use argument matchers with methods that cannot be mocked. Following methods cannotbe stubbed/verified: final/private/equals()/hashCode(). Mocking methods declared on non-public parent classes is not supported.
您不能在验证或存根之外使用参数匹配器。参数匹配器的正确使用示例:when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); 验证(模拟)。someMethod(包含(“foo”))
如果最后一个匹配器返回像 any() 这样的对象但存根方法签名需要原始参数,则此消息可能出现在 NullPointerException 之后,在这种情况下,使用原始替代方法。当(mock.get(any())); // 使用不当,会引发 NPE when(mock.get(anyInt())); // 正确使用
此外,此错误可能会出现,因为您将参数匹配器与无法模拟的方法一起使用。以下方法不能被存根/验证:final/private/equals()/hashCode()。不支持在非公共父类上声明的模拟方法。
Can anyone please help me understand what am I doing wrong in the above code?
谁能帮我理解我在上面的代码中做错了什么?
Note: I am using TestNG and Mockito. I can extend
AbstractTestNGSpringContextTests
and use a spring-test.xml
, declare my
beans and autowire applicationContext. I feel that is an overkill for
my use case. I need to just mock applicationContext's getBean method.
注意:我使用的是 TestNG 和 Mockito。我可以扩展
AbstractTestNGSpringContextTests
和使用spring-test.xml
, 声明我的 bean 和自动装配 applicationContext。我觉得这对我的用例来说太过分了。我只需要模拟 applicationContext 的 getBean 方法。
采纳答案by juherr
The issue comes from doReturn(any(Random.class))
where you are not allowed to use any()
.
Just replace it by a real instance.
问题来自doReturn(any(Random.class))
您不允许使用的地方any()
。只需将其替换为真实实例即可。