27

我有如下方法,

public void generateCSVFile(final Date billingDate) {
    asyncTaskExecutor.execute(new Runnable() {
        public void run() {
            try {
                accessService.generateCSVFile(billingDate);
            } catch (Exception e) {
                LOG.error(e.getMessage());
            }
        }
    });
}

我嘲笑过:

PowerMockito.doNothing().when(accessService).generateCSVFile(billingDate);

但是当我验证时:

verify(rbmPublicViewAccessService, timeout(100).times(1)).generateCSVFile(billingDate);

它给了我没有被调用的感觉。这是因为它是通过单独的线程调用的,是否可以验证在不同线程中调用的方法?

4

3 回答 3

42

当您验证调用时,很可能Runnable尚未执行asyncTaskExecutor,导致您的单元测试中出现验证错误。

解决此问题的最佳方法是加入生成的线程并在验证调用之前等待执行。

如果您无法获取线程的实例,可能的解决方法是模拟asyncTaskExecutor并实现它,以便它将直接执行可运行对象。

private ExecutorService executor;

@Before
public void setup() {
    executor = mock(ExecutorService.class);
    implementAsDirectExecutor(executor);
}

protected void implementAsDirectExecutor(ExecutorService executor) {
    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Exception {
            ((Runnable) invocation.getArguments()[0]).run();
            return null;
        }
    }).when(executor).submit(any(Runnable.class));
}
于 2013-06-20T15:48:58.340 回答
15

我遇到了同样的问题并使用了超时参数 http://javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/Mockito.html#22 但参数 0 像

verify(someClass, timeout(0)).someMethod(any(someParameter.class));

它有效。我假设测试线程产生,因此另一个线程有机会完成它的工作,适当地调用模拟。仍然闻起来像黑客。

于 2014-10-26T14:22:40.353 回答
3

为了进一步迭代 Tom 的答案 - 使用 Java 8 Lambdas,您现在可以使用以下代码来模拟 Executor,这会稍微简洁一些:

    doAnswer((Answer<Void>)invocation -> {
        ((Runnable)invocation.getArgument(0)).run();
        return null;
    }).when(executorService).submit(any(Runnable.class));
于 2019-04-04T13:31:57.583 回答