Skip to content

suggestion: assertException #8

Description

@yshavit

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions