Cannot start JUnit test case containing streams from Eclipse

I am running a JUnit test case from Eclipse 3.4.1. This test case creates a class that starts a thread to do something. When the testing method finishes, it seems that Eclipse is forcibly shutting down the thread.

If I run the same test from the command line, the thread is working correctly.

Somehow I don’t remember how I had encountered 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 several cnt prints (before 1800), and then the test case exits utomatically. However, if I run the main method that runs the JUnit TestRunner, then the thread is counted indefinitely.

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);
  }
}
+3
3

JUnit NG : .

public class ThreadTest {

  static Runnable run = new Runnable() {
    public void run() {
      int cnt = 0;
      while (true)
        System.out.println(cnt++);
    }
  };

  @Test
  public void threadRun() {
    Thread t = new Thread(run);
    t.start();
    assertEquals("RUNNABLE", t.getState().toString());
  }

}

JUnit jar (4.3.1 ) Eclipe , , Eclipse ( :)).

JUnit 4.6 ( ) , ! , Eclipse


, , . , , JUitit:
. , . , , .
, .

JUnit . Unit test . , , .

, , assertXXX. .
: !

+3

, .

(, )

public class ThreadTest {

static Runnable run = new Runnable() {
public void run() {
  int cnt = 0;
  while (true)
    System.out.println(cnt++);
}
};

@Test
public void threadRun() {
Thread t = new Thread(run);
t.start();  

//Run the thread, t, for 30 seconds total. 
//Assert the thread state is RUNNABLE, once per second
for(int i=0;i<30;i++){ 
    assertEquals("RUNNABLE", t.getState().toString());
    try {
        Thread.sleep(1000);//1 second sleep
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
System.out.println("Done with my thread unit test.");
 }
}
+1

, .

protected boolean monitorSecondaryThread(String threadName, StringBuilder errorMessage, boolean ignoreFailSafe) {
    int NUM_THREADS_BESIDES_SECONDARY_THREAD = 2;
    int MAX_WAIT_TIME = 10000;

    MyUncaughtExceptionHandler meh = new MyUncaughtExceptionHandler();
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    for (Thread t : threadSet) {
        t.setUncaughtExceptionHandler(meh);
    }

    Date start = Calendar.getInstance().getTime();

    boolean stillAlive = true;
    while (stillAlive) {
        for (Thread t : threadSet) {
            if (t.getName().equalsIgnoreCase(threadName) && !t.isAlive()) {
                stillAlive = false;
            }
        }

        Date end = Calendar.getInstance().getTime();

        if (!ignoreFailSafe && (end.getTime() - start.getTime() > MAX_WAIT_TIME || Thread.activeCount() <= NUM_THREADS_BESIDES_SECONDARY_THREAD)) {
            System.out.println("Oops, flawed thread monitor.");
            stillAlive = false;
        }
    }


    if (meh.errorCount > 0) {
        System.out.println(meh.error);
        errorMessage.append(meh.error);
        return false;
    }

    return true;
}

private class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {

    public int errorCount = 0;
    public String error = "";

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(bs);
        e.printStackTrace(ps);
        error = bs.toString();
        errorCount++;
    }

}
+1
source

Source: https://habr.com/ru/post/1706587/


All Articles