Cannot run a JUnit test case containing threads from Eclipse
- by Parag
I am running JUnit test case from Eclipse 3.4.1 . This test case creates a class which starts a thread to do some stuff. When the test method ends it seems that Eclipse is forcibly shutting down the thread.
If I run the same test from the command line, then the thread runs properly.
Somehow I do not remember running into such problems with Eclipse before. Is this something that was always present in Eclipse or did they add it in 3.4.x ?
Here is an example:
When I run this test from Eclipse, I get a few printts of the cnt (till about 1800) and then the test case is terminated utomatically. However, if I run the main method, which starts JUnit's TestRunner, then the thread counts indefinetely.
import junit.framework.TestCase;
import junit.textui.TestRunner;
/**
 * This class shows that Eclipses JUnit test case runner will forcibly 
 * terminate all running threads
 * 
 * @author pshah
 *
 */
public class ThreadTest extends TestCase {
  static Runnable run = new Runnable() {
    public void run() {
      int cnt = 0;
      while(true) System.out.println(cnt++);
    }
  };
  public void testThread() {
    Thread t = new Thread(run);
    t.start();
  }
  public static void main(String args[]) {
    TestRunner runner = new TestRunner();
    runner.run(ThreadTest.class);
  }
}