I have some Java code that I am unit testing using JUnit, running inside Eclipse (Neon). It so happens that some code under test has a bug in it, causing it to enter an infinite loop. The JUnit run then, of course, does not finish. How do I kill the test run?
The button for stopping the test run ("Stop JUnit Test Run") does not work well: the GUI seems to think that it has stopped the test run, but a look at the CPU activity (using top
, for example), shows that a Java thread is still running. I can kill the thread myself by sending it a kill signal. But that seems a kludge and is inconvenient. Is there a better way, available within Eclipse itself?
Kill it from the console view, using the red button. This stops the process. Stopping it from the junit view only asks it to stop. Save this answer.
The best way to disable a test method is to use the @Disabled annotation. @Disabled annotation is used to tell JUnit 5 engine that the annotated test class or test method is currently disabled and should not be executed.
For newer versions of Eclipse: open the Debug perspective (Window > Open Perspective > Debug) select process in Devices list (bottom right) Hit Stop button (top right of Devices pane)
Kill it from the console view, using the red button. This stops the process.
Stopping it from the junit view only asks it to stop.
You can handle such things with JUnit by specifying a time-out within
the @Test
annotation. For example:
// Simple test-case which will always fail with time-out
@Test(timeout = 1000 * 60) throws Exception // 60 seconds
public void testSomething() {
for (int i = 0; i < 100; i++) { // 100 seconds
Thread.sleep(1000);
}
}
If your test method doesn't finish on time, then JUnit will interrupt it and report a test failure. For the example above it will be:
java.lang.Exception: test timed out after 60000 milliseconds
at java.lang.Thread.sleep(Native Method)
at my.package.Test1.testSomething(Test1.java:12)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With