title: Java Thread.currentThread().setUncaughtExceptionHandler() not working with JUNIT?
body: <p>In the example code below, if the testMethod() is run via main() it works as expected but if it is run via JUNIT then MyUncaughtExceptionHandler does not get called.</p>
  
  <p>Is there some explanation for this?</p>
  
  <pre><code>package nz.co.test;
  
  import java.lang.Thread.UncaughtExceptionHandler;
  
  import org.junit.Test;
  
  public class ThreadDemo {
  
    private void testMethod() {
      Thread.currentThread().setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
  
      Object b = null;
      // Cause a NPE
      b.hashCode();
    }
  
    @Test
    public void testJunit() {
      // Run via JUnit and MyUncaughtExceptionHandler doesn't catch the Exception
      testMethod(); 
    }
  
    public static void main(String[] args) {
      // Run via main() works as expected
      new ThreadDemo().testMethod();
    }
  
    static class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
  
      @Override
      public void uncaughtException(Thread t, Throwable e) {
        System.out.println("I caught the exception");
      } 
    }
  }
  </code></pre>