Lin*_*inh 6 java junit timeout mockito
我是Mockito和JUnit的新手,并尝试了解这些框架的基本单元测试。JUnit和Mockito中的大多数概念似乎都很简单易懂。但是,我陷入了timeoutMockito的困境。是否timeout在玩的Mockito同样的作用,因为它在JUnit的呢?贝娄是我的代码。
@Mock Timeoutable timeoutable;
@Test(timeout = 100)
public void testJUnitTimeout() {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
}
}
@Test
public void testMockitoTimeout(){
doAnswer(new Answer() {
@Override public Object answer(InvocationOnMock invocation){
try {
Thread.sleep(1000);
} catch (InterruptedException ie){
}
return null;
}
}).when(timeoutable).longOperation();
timeoutable.longOperation();
verify(timeoutable, timeout(100)).longOperation();
}
Run Code Online (Sandbox Code Playgroud)
我希望两个测试都失败。但只有testJUnitTimeout()失败。为什么第二次考试通过?
非常感谢你。
带有超时的验证旨在用于验证是否已在指定的超时时间内同时调用了该操作。
它为并行操作提供了一种有限形式的验证。
下面的示例演示了此行为:
private final Runnable asyncOperation = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
timeoutable.longOperation();
} catch (InterruptedException e) {
}
}
};
@Test
public void testMockitoConcurrentTimeoutSucceeds(){
new Thread(asyncOperation).start();
verify(timeoutable, timeout(2000)).longOperation();
}
@Test
public void testMockitoConcurrentTimeoutFails(){
new Thread(asyncOperation).start();
verify(timeoutable, timeout(100)).longOperation();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9611 次 |
| 最近记录: |