在 mockito 中获取 InvalidUseOfMatchersException



我写了这个java方法:

public int run(String jobName) {
}

我写了这个测试代码:

@Test
public void testBatchStatusUpdateWithOneCompleteStatus() {
    Set<BatchEntity> staleBatch = createStaleBatch();
    Set<Integer> activeBatch = createActiveBatch();

    when(batchRepository.findBatchIdByStateIn(
            (Arrays.asList(BatchStates.IN_PROGRESS,
                    BatchStates.INTENT_MARKED)))).thenReturn(staleBatch);
    when(listingRepository.findBatchId()).thenReturn(activeBatch);
    Assert.assertEquals(batchStatusUpdate.run(Mockito.any(String.class)), 1);
    Mockito.verify(batchRepository,Mockito.times(2)).save(Mockito.any(BatchEntity.class));
}

当我运行

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:  Invalid use of argument matchers! 0 matchers expected, 1 recorded: at backgroundjob.BatchStatusUpdateTest.testBatchStatusUpdateWithOneCompleteStatus This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

有关更多信息,请参阅 javadoc for Matchers 类。

当运行方法具有字符串参数时,我收到此错误。当我从运行方法中删除字符串参数时,测试用例被传递。

Mockito.any()创建一个匹配器,在指定模拟或使用Mockito.verify()验证呼叫时使用。但是,它们作为您正在测试的方法的参数没有意义。您的调用应更改为使用实际字符串,例如Assert.assertEquals(batchStatusUpdate.run("some value"), 1);而不是Assert.assertEquals(batchStatusUpdate.run(Mockito.any(String.class)), 1);

相关内容

  • 没有找到相关文章

最新更新