Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a JUnit test run in Eclipse that has entered an infinte loop

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?

like image 575
Raedwald Avatar asked Aug 05 '13 11:08

Raedwald


People also ask

How do I stop JUnit test in Eclipse?

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.

How do I stop JUnit testing?

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.

How do I stop a process from running in Eclipse?

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)


2 Answers

Kill it from the console view, using the red button. This stops the process.

enter image description here

Stopping it from the junit view only asks it to stop.

like image 90
Matthew Farwell Avatar answered Sep 28 '22 14:09

Matthew Farwell


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)
like image 45
Thomas Fritsch Avatar answered Sep 28 '22 14:09

Thomas Fritsch