java - How does Mockito timeout work? -
i'm new mockito , junit , try understand basic unit testing these frameworks. concepts in junit , mockito seem straightforward , understandable. however, got stuck timeout
in mockito. timeout
in mockito play same role in junit? bellow code.
@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(); }
i expected both tests failed. testjunittimeout()
failed. why second test pass?
thank much.
verification timeout intended used verify whether or not operation has been invoked concurrently within specified timeout.
it provides limited form of verification concurrent operations.
the following examples demonstrate behaviour:
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(); }
Comments
Post a Comment