我试着用Mockito模拟一个类的行为。这使用了Mockito 1.x。迁移到JUnit 5和Mockito 2似乎不再起作用了。
@ExtendWith(MockitoExtension.class)
public class MockitoExample {
static abstract class TestClass {
public abstract int booleanMethod(boolean arg);
}
@Mock
TestClass testClass;
@BeforeEach
public void beforeEach() {
when(testClass.booleanMethod(eq(true))).thenReturn(1);
when(testClass.booleanMethod(eq(false))).thenReturn(2);
}
@Test
public void test() {
assertEquals(1,testClass.booleanMethod(true));
assertEquals(2,testClass.booleanMethod(false));
}
}所期望的是,模拟的TestClass显示测试方法中测试的行为。
我得到的错误是:
org.mockito.exceptions.misusing.PotentialStubbingProblem:
Strict stubbing argument mismatch. Please check:
- this invocation of 'booleanMethod' method:
testClass.booleanMethod(false);
-> at org.oneandone.ejbcdiunit.mockito_example.MockitoExample.beforeEach(MockitoExample.java:30)
- has following stubbing(s) with different arguments:
1. testClass.booleanMethod(false);
-> at org.oneandone.ejbcdiunit.mockito_example.MockitoExample.beforeEach(MockitoExample.java:29)
Typically, stubbing argument mismatch indicates user mistake when writing tests.
Mockito fails early so that you can debug potential problem easily.
However, there are legit scenarios when this exception generates false negative signal:
- stubbing the same method multiple times using 'given().will()' or 'when().then()' API
Please use 'will().given()' or 'doReturn().when()' API for stubbing.
- stubbed method is intentionally invoked with different arguments by code under test
Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).
For more information see javadoc for PotentialStubbingProblem class.在这两种情况下,论点false似乎是匹配的,尽管我显然与true匹配。
是Mockito 2.17中的一个错误还是一个误会。我应该如何使用Mockito2.x来模拟具有不同布尔参数的调用?
示例也可以在github上找到。但是,“肯定火”只会使用
mvn test -Dtest=MockitoExample使用Mockito 2.21执行测试将得到相同的结果。
发布于 2018-09-03 22:10:30
由于Mockito 2.20也可以在本地添加宽大()
@ExtendWith(MockitoExtension.class)
public class MockitoExample {
static abstract class TestClass {
public abstract int booleanMethod(boolean arg);
}
@Mock
TestClass testClass;
@BeforeEach
public void beforeEach() {
lenient().when(testClass.booleanMethod(eq(true))).thenReturn(1);
lenient().when(testClass.booleanMethod(eq(false))).thenReturn(2);
}
@Test
public void test() {
assertEquals(1,testClass.booleanMethod(true));
assertEquals(2,testClass.booleanMethod(false));
}
}发布于 2018-09-02 19:43:42
使用严格的存根(Mockito的默认行为),在同一个方法上调用几个when将重置模拟。解决方案是调用when (一次)并在Answer中具有逻辑
@BeforeEach
public void beforeEach() {
when(testClass.booleanMethod(anyBoolean())).thenAnswer(invocationOnMock -> {
if ((boolean) invocationOnMock.getArguments()[0]) {
return 1;
}
return 2;
});
}或者,您可以使用宽松的嘲讽,但这并不总是一个好主意--宽容的嘲弄允许冗余的固执,使您更容易在测试中出错,这可能会导致“生产”代码中未被注意到的错误:
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class MockitoExample {发布于 2018-09-03 10:21:11
由于第一个答案出乎我的意料,我检查了以下内容:
interface Poops {
String get(boolean is);
}
@Test
void test1() {
Poops a = mock(Poops.class);
when(a.get(eq(true))).thenReturn("1");
when(a.get(eq(false))).thenReturn("2");
Assertions.assertEquals("1", a.get(true));
Assertions.assertEquals("2", a.get(false));
}它适用于Mockito 2.21.0。
更新:问题似乎是木星Mockito扩展,它将默认设置更改为Strictness.STRICT_STUBS。
https://stackoverflow.com/questions/52139619
复制相似问题