Obviously, setUncaughtExceptionHandler sets a handler for uncaught exceptions. But JUnit catches all the exceptions thrown from the testing methods.
Anyway, this is a weird way to do a unit test. Unit test should check your code, not the JVM specification.
I imagine Unit test as follows:
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());
source share