Mockito验证仍然无法使用所有匹配器
我最近正在编写JUNIT测试,我承认Mockito要么需要所有原始值或使用所有匹配项,但我发现以下情况仍然失败,所有匹配项都失败了,错误消息是:
对参数匹配者的使用无效! 0个匹配项预期,1个记录:
我进行了一些测试,这似乎与使用存根方法作为eq()匹配器的值有关。请参阅下面的示例:
我有一个非常简单的
public class A {
public void testMockitoMatcher(double a, String b){
}
}
这是我的测试用例
import org.junit.Test;
import static org.mockito.Mockito.*;
public class SomeUnitTest {
private A mockA = mock(A.class);
@Test
public void allMatchersDoesntWork() {
Object mockObject = mock(Object.class);
String someString = "Just some mocked String value to return";
when(mockObject.toString()).thenReturn(someString);
mockA.testMockitoMatcher(1312d, mockObject.toString());
verify(mockA, times(1)).testMockitoMatcher(anyDouble(), eq(someString)); //<- This works
verify(mockA, times(1)).testMockitoMatcher(anyDouble(), eq(mockObject.toString())); //<- This doesn't by using stub method toString() to return the String value as param to eq()
}
}
我还通过在第二个验证语句上使用debug验证了opobobject.tostring()仍然可以返回对我来说是个价值。另外,两者都验证使用所有匹配器,为什么Mockito仍然只给我1个匹配者,而不是在第二个验证中录制2个匹配器?
谢谢!
I am recently writing a JUnit test and I acknowledged that Mockito either needs all Raw values or using all Matchers but I find the following case still fails with all Matchers, error message is:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
I tested a little bit and it looks like something to do with using a stub method as value for the eq() Matcher. See example below:
I have a Class A very simple
public class A {
public void testMockitoMatcher(double a, String b){
}
}
Here is my test case
import org.junit.Test;
import static org.mockito.Mockito.*;
public class SomeUnitTest {
private A mockA = mock(A.class);
@Test
public void allMatchersDoesntWork() {
Object mockObject = mock(Object.class);
String someString = "Just some mocked String value to return";
when(mockObject.toString()).thenReturn(someString);
mockA.testMockitoMatcher(1312d, mockObject.toString());
verify(mockA, times(1)).testMockitoMatcher(anyDouble(), eq(someString)); //<- This works
verify(mockA, times(1)).testMockitoMatcher(anyDouble(), eq(mockObject.toString())); //<- This doesn't by using stub method toString() to return the String value as param to eq()
}
}
I also verified by using debug at 2nd verify statement that mockObject.toString() can still return the stubbed value for me. Also both verify use all Matchers, why mockito still gives me only 1 Matchers recorded instead of 2 at the 2nd verify?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mockito希望您的测试处于三个阶段。
您需要按照此顺序进行这三件事,因为Mockito跟踪您是否正在固执,验证或其他任何内容。它还具有自己的内部堆栈,可存储匹配器,用于固定或验证。
在您的第二个
验证
在您的示例中调用anydouble()
将Matcher
调用。但这为调用mockobject.tostring()
创建了无效的状态。不要这样做。一旦准备好进行断言和验证,您就不应该再拨打任何对固定方法的电话了。将每个测试的“安排”,“ ACT”和“断言”阶段完全彼此分开。
Mockito expects your tests to be in three stages.
You need to do these three things, in this sequence, because Mockito tracks whether you're stubbing, verifying or whatever. It also has its own internal stack which stores Matchers, for use in stubbing or verification.
In your second
verify
call in your example, the call toanyDouble()
puts theMatcher
onto the stack. But this creates an invalid state for the call tomockObject.toString()
.Don't do this. Once you're ready to run your assertions and verifications, you shouldn't be making any more calls to stubbed methods. Keep the "arrange", "act" and "assert" stages of each test entirely separate from each other.