Reputation: 35276
Test in my app is throwing:
java.lang.AssertionError: Expected exception: java.lang.RuntimeException
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:35)
Even if I have this:
@Test(expected=RuntimeException.class)
public void test(){
// I can see from the console log that a java.lang.RuntimeException was thrown
}
What could be the issue?
Upvotes: 1
Views: 5430
Reputation: 12527
By annotating your test with
@Test(expected=RuntimeException.class)
you are asserting that when the test is run, a RuntimeExcetption will be thrown and not caught. The fact that you have stated in the comments that "there is a try-catch statement in the code that was called", implies that the exception is caught.
Thus JUnit is right to flag this as a failed test. You are asserting that an exception is thrown, but one is not.
Upvotes: 2
Reputation: 279880
The only way for JUnit to detect that an exception was thrown is for the @Test
method to throw it, ie. terminate its execution because of it.
If you're catching the exception and handling, method execution won't stop because of a thrown exception. It will complete normally. You need it to complete abruptly because of the thrown exception. Rethrow it if you need to log it.
Upvotes: 1