Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestion: assertException #499

Open
yshavit opened this issue Mar 25, 2014 · 1 comment
Open

suggestion: assertException #499

yshavit opened this issue Mar 25, 2014 · 1 comment

Comments

@yshavit
Copy link

yshavit commented Mar 25, 2014

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.

@lbergelson
Copy link

This is a good idea. I would use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants