I know about @Test(expectedExceptions=...), but with Java 8 officially out, testng could provide more fine-tuned asserting. I'm thinking of something like:
public interface ExceptionalRunnable {
public void run() throws Exception;
}
assertException(ExceptionalRunnable runnable, Class<? extends Throwable> expected) {
...
}
This would be backwards-compatible, though probably too verbose to be very useful for Java 7 or previous. But in Java 8, you can succinctly write something like:
assertException( () -> myWhatever.doFoo(), MyExpectedException.class);
This finer-grained control would help cut down on false positives. For instance, let's say you're expecting an NPE. If the test itself contained an NPE, it would pass even if the actual method you want to test didn't NPE -- possibly before it even got invoked. If you used assertException instead, and didn't include expectedExceptions in the @Test annotation, the test would fail as it should.
I know about
@Test(expectedExceptions=...), but with Java 8 officially out, testng could provide more fine-tuned asserting. I'm thinking of something like:This would be backwards-compatible, though probably too verbose to be very useful for Java 7 or previous. But in Java 8, you can succinctly write something like:
This finer-grained control would help cut down on false positives. For instance, let's say you're expecting an NPE. If the test itself contained an NPE, it would pass even if the actual method you want to test didn't NPE -- possibly before it even got invoked. If you used
assertExceptioninstead, and didn't includeexpectedExceptionsin the@Testannotation, the test would fail as it should.