Mockito框架@Mock和之间有什么区别@InjectMocks?
争论的Mockito匹配器(如any,argThat,eq,same,和ArgumentCaptor.capture())表现非常不同,从Hamcrest匹配器.
Mockito匹配器经常导致InvalidUseOfMatchersException,即使在使用任何匹配器后执行很长时间的代码中也是如此.
Mockito匹配器受到奇怪的规则的影响,例如,如果给定方法中的一个参数使用匹配器,则只需要对所有参数使用Mockito匹配器.
当覆盖Answers或使用(Integer) any()等时,Mockito匹配器可能会导致NullPointerException .
使用Mockito匹配器以某种方式重构代码可能会产生异常和意外行为,并且可能完全失败.
为什么Mockito匹配器是这样设计的,它们是如何实现的?
我正在使用Mockito进行单元测试,我得到以下异常.
org.mockito.exceptions.base.MockitoException:
`'setResponseTimeStampUtc'` is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
doThrow(exception).when(mock).someVoidMethod();
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. The method you are trying to stub is *overloaded*. Make sure you are calling the right overloaded version.
2. Somewhere in your test you are stubbing *final methods*. Sorry, Mockito does not verify/stub …Run Code Online (Sandbox Code Playgroud) 发现非常有趣的问题,并在调试后发现了重现它的场景.
所以,如果我有一个包含范围B的类,它有一些公共方法,公共类A扩展它:
package somepackage;
class B {
public void someMethod() {
throw NullPointerException();
}
}
package somepackage;
public class A extends B {
}
Run Code Online (Sandbox Code Playgroud)
然后在测试中:
A a = mock(A.class);
a.someMethod();
Run Code Online (Sandbox Code Playgroud)
并猜测是什么,我得到了刚刚扔的NullPointerException,所以Mockito以某种方式创建了一个"真正的"对象并调用了一个真正的方法而不是模拟的方法.为什么这样?
java.lang.IllegalArgumentException
at test.B.setProxy(B.java:6)
at test.A.setProxy(A.java:1)
at secretservice.service.TestFDSServiceImpl.testService(TestFDSServiceImpl.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at …Run Code Online (Sandbox Code Playgroud)