[英]Unit test for Runnable with Mockito
我有一个这样的代码,我想为其编写单元测试。
public class TestClass {
private final Executor executor;
private final Handler handler;
TestClass(Executor executor, Handler handler) {
this.executor = executor;
this.handler = handler;
}
void doSomething(String param1) {
executor.execute(new Runnable() {
@Override
public void run() {
//do something
handler.callHandler();
}
});
}
}
如何使用 Mockito / Powermockito 来验证 callHandler() 方法是否被调用。
将模拟Handler
传递给TestClass
的构造函数。
然后使用Mockito.verify()
断言调用了callHandler()
方法。
您可以存储一个在CountDownLatch
上倒计时的答案,以使测试等待处理程序被命中。 等待将涉及设置合理的超时,这可能会非常棘手,你不希望它太高,或失败会使试运行更长的时间,而不是太低了,这样你就不会得到误报。
Handler handler = mock(Handler.class);
CountDownLatch finished = new CountDownLatch(1);
doAnswer(invocation -> {
finished.countDown();
return null;
}).when(handler).callHandler();
TestClass testClass = new TestClass(executor, handler);
testClass.doSomething("thisThing");
boolean ended = finished.await(10, TimeUnit.SECONDS);
assertThat(ended).isTrue();
verify(handler).callHandler();
如果您只是尝试确定是否调用了处理程序,则可以使用在同一线程上执行的Executor
。 这将使测试更加稳定。
Handler handler = mock(Handler.class);
Executor executor = new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
};
TestClass testClass = new TestClass(executor, handler);
testClass.doSomething("thisThing");
verify(handler).callHandler();
处理并发问题的另一种方法是模拟 Executor 在调用时“什么都不做”,并在测试中使用ArgumentCaptor
来捕获它会调用的 Runnable。 拥有 Runnable 后,您可以在与测试相同的线程中手动调用它。
下面是一个例子:
@Mock
private Executor executor;
@Mock
private Handler handler;
@Before
public void setup() {
openMocks(this);
doNothing().when(executor).execute(any());
}
@Test
public void runTest() {
TestClass testClass = new TestClass(executor, handler);
testClass.doSomething("the thing");
ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
verify(executor).execute(runnable.capture());
Runnable capturedRunnable = runnable.getValue();
capturedRunnable.run();
verify(handler).callHandler();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.