7

在下面的示例代码中,如果 testMethod() 通过 main() 运行,它会按预期工作,但如果它通过 JUNIT 运行,则不会调用 MyUncaughtExceptionHandler。

对此有什么解释吗?

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");
    } 
  }
}
4

2 回答 2

7

显然,setUncaughtExceptionHandler未捕获异常的集合处理程序。但是 JUnit 会捕获从测试方法抛出的所有异常。

无论如何,进行单元测试是一种奇怪的方式。单元测试应该测试你的代码,而不是 JVM 规范。

我想像这样的单元测试:

public class MyUncaughtExceptionHandlerTest
{
  @Mock Thread thread;
  MyUncaughtExceptionHandler testObject = new MyUncaughtExceptionHandler();

  @Before
  public void setUp()
  {
     MockitoAnnotations.initMocks(this);
  }

  @Test
  public void handleNpeShouldDoOneThing()
  {
    testObject.handleException(thread, new NullPointerException());
    //verify(oneThing)
  }

  @Test
  public void handleOomShouldDoSomethingElse()
  {
    testObject.handleException(thread, new OutOfMemoryError());
    //verify(somethingElse)
  }
}
于 2013-08-01T12:45:15.353 回答
6

这是因为测试中抛出的所有异常都被 JUnit 捕获和处理,所以 UncaughtExceptionHandler 不会得到任何未捕获的异常。它在 org.junit.runners.ParentRunners 中完成

...
    protected final void runLeaf(Statement statement, Description description,
            RunNotifier notifier) {
        EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description);
        eachNotifier.fireTestStarted();
        try {
            statement.evaluate();   <-- test method execution is called from here
        } catch (AssumptionViolatedException e) {
            eachNotifier.addFailedAssumption(e);
        } catch (Throwable e) {
            eachNotifier.addFailure(e);
        } finally {
            eachNotifier.fireTestFinished();
        }
    }
于 2013-08-01T13:06:54.530 回答