首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Mockito 2对服务进行仿真会导致顽固错误

使用Mockito 2对服务进行仿真会导致顽固错误
EN

Stack Overflow用户
提问于 2018-09-02 17:51:03
回答 3查看 81.9K关注 0票数 42

我试着用Mockito模拟一个类的行为。这使用了Mockito 1.x。迁移到JUnit 5和Mockito 2似乎不再起作用了。

代码语言:javascript
复制
@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显示测试方法中测试的行为。

我得到的错误是:

代码语言:javascript
复制
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上找到。但是,“肯定火”只会使用

代码语言:javascript
复制
mvn test -Dtest=MockitoExample

使用Mockito 2.21执行测试将得到相同的结果。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-03 22:10:30

由于Mockito 2.20也可以在本地添加宽大()

代码语言:javascript
复制
@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));
  }
}
票数 45
EN

Stack Overflow用户

发布于 2018-09-02 19:43:42

使用严格的存根(Mockito的默认行为),在同一个方法上调用几个when将重置模拟。解决方案是调用when (一次)并在Answer中具有逻辑

代码语言:javascript
复制
@BeforeEach
public void beforeEach() {
    when(testClass.booleanMethod(anyBoolean())).thenAnswer(invocationOnMock -> {
        if ((boolean) invocationOnMock.getArguments()[0]) {
            return 1;
        }
        return 2;
    });
}

或者,您可以使用宽松的嘲讽,但这并不总是一个好主意--宽容的嘲弄允许冗余的固执,使您更容易在测试中出错,这可能会导致“生产”代码中未被注意到的错误:

代码语言:javascript
复制
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class MockitoExample {
票数 40
EN

Stack Overflow用户

发布于 2018-09-03 10:21:11

由于第一个答案出乎我的意料,我检查了以下内容:

代码语言:javascript
复制
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

票数 -4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52139619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档