Mockito验证Callable对象没有失败

时间:2017-07-17 22:12:17

标签: java unit-testing mocking mockito callable

对于对Callable参数执行操作的方法,我有以下代码:

public static <T> T queryWithRetry(Callable<T> query, int maxTries, int retryIntervalInMilliseconds) throws MongoServiceException, InterruptedException {
    int tries = MAX_TRIES;
    while (tries-- > 0) {
      try {
        return query.call();
      } catch (TimeoutException e) {
        LOGGER.debug(String.format("Query timed out. Retrying attempt %d/%d", MAX_TRIES - tries, MAX_TRIES));
        Thread.sleep(RETRY_INTERVAL_IN_MILLISECONDS);
        continue;
    }
    throw new RandomException();
  }

我正在使用Mockito尝试验证线query.call()在投掷MAX_TRIES之前的次数是RandomException次。我尝试使用以下测试代码执行此操作:

public class CallableQueryTest {
  private static final int MAX_TRIES = 3;
  private static final int RETRY_INTERVAL_IN_MILLISECONDS = 100;

  @Mock
  private Callable<Document> mockCallable;

  @Rule
  public ExpectedException thrown = ExpectedException.none();

  @Before
  public void setUp() throws Exception {
    mockCallable = Mockito.mock(Callable.class);
  }

  @Test
  public void testQueryConfigThrowsRandomExceptionOnTimeout() throws Exception {
    Mockito.when(mockCallable.call()).thenThrow(new TimeoutException("timeout"));

    thrown.expect(RandomException.class);    
    Mockito.verify(mockCallable, Mockito.atMost(1)).call();
    MongoQueryUtils.queryWithRetry(mockCallable, MAX_TRIES, RETRY_INTERVAL_IN_MILLISECONDS);
  }
}

Mockito代码成功测试了该方法抛出RandomException,但不正确地说测试通过了。

此测试应该失败,因为我写道Mockito应该验证mockCallable.call()最多执行一次,但据我所知,它被称为MAX_TRIES次(设置为3)。

有人可以解释这种行为并就如何正确测试mockCallable.call()被调用的次数提出建议吗?

1 个答案:

答案 0 :(得分:2)

您的测试有充足错误。这是你想要的东西:

private static final int MAX_TRIES = 3;
private static final int RETRY_INTERVAL_IN_MILLISECONDS = 100;

@Mock
private Callable<String> mockCallable;

@Test
public void testQueryConfigThrowsRandomExceptionOnTimeout() throws Exception {
    when(mockCallable.call()).thenThrow(new IllegalArgumentException("timeout"));
    try {
        queryWithRetry(mockCallable, MAX_TRIES, RETRY_INTERVAL_IN_MILLISECONDS);
        fail("should have thrown");
    } catch (RuntimeException re) {
        // as expected
    }
    verify(mockCallable, Mockito.times(3)).call();
}

(请注意:我更改了测试的异常类型;但这应该是显而易见的。)

所以,你错了:

  • 我建议使用@RunWith(MockitoJUnitRunner.class),然后使用@Mock注释。你有@Mock注释加上一个为同一个字段调用Mockito.mock()的设置方法。这是多余的。可以将注释与@RunWith一起使用,也可以在设置方法中调用MockitoAnnotations.initMocks()
  • verify()必须在您与模拟对象进行交互之后被称为 。想想那个有柜台的模拟器。在你进行交互之前,计数器都是0.所以在你触发对模拟的调用之前,没有必要在模拟其计数器之前
  • 我不熟悉JUnit规则;因此我重写了测试,没有它。如果要使用JUnit规则进行异常处理;留给读者作为锻炼。