Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Junit shut down all the threads?

I have a Junit class with 6 tests in them. When the first test runs, it calls one of the methods in my other class which starts a new Thread(). Let's call it MyThread.

The idea is for MyThread to keep running for a bit longer after the JUnit tests are done.

For example, for all the JUnit tests to be done, it would take 1 secs, and so I would set MyThread to run for 3 secs:

new Thread(new Runable(){
    public void run(){
        System.out.println("start");
        Thread.sleep(3000);
        System.out.println("end");
    }
}).start();

The start would be shown on the standard output, but the end is never shown.

It seems to me that whenever the JUnit tests are done, MyThread is shut down as well.

So I am wondering, does JUnit shut down all the threads it initiates? Thanks a lot.

like image 222
Will Avatar asked Jun 09 '26 21:06

Will


1 Answers

This is actually not JUnit but the external systems which run JUnit test (like Eclipse or Maven) who are responsible for terminating JVM. Those call System.exit which stops all the threads. If JUnit did it then the external system would have no chance to process the results.

like image 185
Evgeniy Dorofeev Avatar answered Jun 11 '26 12:06

Evgeniy Dorofeev