Reputation: 1
I have a Java program where I am trying to Fail my test at the end with Assert.fail(), but at the end of the program, the threads keeps running and program does not end.
@Test()
void function(){
try {
post_call = service.submit(new Counter(asyncUrl, input,transaction_id));
get_call = service.submit(new PathScanner(asyncUrl, input, transaction_id));
try {
post_call.get();
} catch (ExecutionException ex) {
Assert.fail();
ex.getCause().printStackTrace();
}
try {
get_call.get();
} catch (ExecutionException ex) {
Assert.fail();
ex.getCause().printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
this is a test method snippet and the test case fails also as expected but the program does not exit after Assert.fail() is called.
If i remove the assert.fail(), then it the program terminates successfully ...its just that it does not show any failure then.
Please suggest a method to terminate all active threads after Assert.fail is called.
Upvotes: 0
Views: 389
Reputation: 873
For terminate any test on Junit 4 add argument on annotation timeout
@Test(timeout=2000)
public void testWithTimeout() {
...
}
Or add rule
@Rule
public Timeout globalTimeout = Timeout.seconds(10); // 10 seconds max per method tested
For Junit 5
@Test
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
void failsIfExecutionTimeExceeds100Milliseconds() {
// fails if execution time exceeds 100 milliseconds
}
More info JUnit 4 Timeout for tests
More info JUnit 5 Timeout for tests
Upvotes: 1