相关疑难解决方法(0)

使用Mockito 2模拟服务会导致存根错误

我尝试使用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));
  }
}
Run Code Online (Sandbox Code Playgroud)

期望的是,模拟的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 …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mocking mockito junit5

13
推荐指数
3
解决办法
5920
查看次数

标签 统计

java ×1

junit5 ×1

mocking ×1

mockito ×1

unit-testing ×1