Andrew Clarke
Andrew Clarke

Reputation: 181

Thread behaving strangely in JUnit

I'm trying to write a unit test that requires mulitple threads. However, it seems that the threads just stop part way through execution. Consider the following code:

public class Test {
    @org.junit.Test
    public void TestThreads() {
        new Thread(new Runnable() {
            public void run() {
                for (int i = 1; i < 1000; i++) System.out.println(i);
            }
        }).start();
    }
}

If I run this unit test, it will generally stop displaying output somewhere between 140-180. If I convert this code into a regular class and run it, it works fine. Does anybody have any idea what I'm missing here?

Thanks, - Andrew.

Upvotes: 18

Views: 9232

Answers (3)

Martin Ellis
Martin Ellis

Reputation: 9651

You can use Thread.join() to prevent the test from finishing before the new thread has completed its task:

@org.junit.Test
public void TestThreads() throws InterruptedException {
    Thread t = new Thread(new Runnable() {
        public void run() {
            for (int i = 1; i < 1000; i++) System.out.println(i);
        }
    });
    t.start();
    t.join();
}

Normally, the JVM will terminate when the last non-daemon thread terminates. You might expect that simply calling t.setDaemon(false) on the thread would prevent the JVM from exiting before the task is finished. However, junit will call System.exit() when the main thread has finished.

As Gus notes in the comments: "you need join() because start() doesn't block".

He's correct that you could also call run() in this minimal example. However, I assume you're starting a thread because you want it to run concurrently with another thread. Calling run() on a Thread is flagged up as a possible mistake by FindBugs - if you're just going to call run(), you'd probably just want to implement Runnable instead of using Thread.

Upvotes: 28

goblinjuice
goblinjuice

Reputation: 3214

In the TestThread function, JUnit has no way of telling that you spawned a new thread. It reclaims any objects it created in the function as soon as the function ends (last line is reached) and doesn't know about the thread.

For this reason, you see Output from the Thread until it gets killed. If you want to wait for Thread Completion, you can use Thread.join (right after you call Thread.start()) to make it wait for the result or use wait-notify on some object.

Also, found this question: JUnit terminates child threads

Upvotes: 6

RNJ
RNJ

Reputation: 15552

It's probably that the parent thread running the test is finishing. If you can avoid it then don't create a new thread in you tests. I would create a new class implementing the runnable and then test that class my simply calling run on that object. You shouldn't need to test thread scheduling as part of your tests. Hopefully the java language dev team have that covered :)

Upvotes: 0

Related Questions