[英]How does Mockito timeout work?
我是Mockito和JUnit的新手,並嘗試了解這些框架的基本單元測試。 JUnit和Mockito中的大多數概念似乎都很簡單易懂。 但是,我在Mockito中遇到了timeout
問題。 Mockito中的timeout
是否與JUnit中的timeout
相同? 貝婁是我的代碼。
@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();
}
我希望兩個測試都失敗。 但是只有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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.