I'm writing a JUnit/Mockito test where an exception is expected to be thrown. Of course I can just do this:
@Test(expected=IllegalArgumentException.class) But it doesn't let me to do anything else after it was thrown. So I was thinking maybe something more like:
Exception actualEx = null; try { // Act sut.doStuff(); } catch (final Exception ex) { actualEx = ex; } // Assert assertTrue(IllegalArgumentException.class.equals(actualEx.getClass())); // ... perhaps verify the exception details verifyNoMoreInteractions(mockObject); This seems pretty ugly and feels like it could be improved - is there a better way?
AssertThrows: How do you assert that a certain exception is thrown in JUnit 4 tests?