Reputation: 1140
I have this sort of JUnit test:
@Test
public void testNullCheck() {
String res = someMethod();
assertThat("This is the someMethodTest", res, is(notNullValue()));
}
If someMethod()
throws an exception I get a stack trace but the "This is the someMethodTest" is not printed as assertThat()
is not called. Is there a somewhat elegant JUnit/hamcrest way to print a custom error message? Eventually I want this in a parametrized test to print the parameter for which the test fails. Note, I don't want to test for a specific exception.
Upvotes: 2
Views: 1259
Reputation: 1140
Using Stefan Birkner's suggestion this is what I came up with. Comments welcome.
package my.test;
import org.junit.internal.AssumptionViolatedException;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class ExceptionCatcher implements TestRule {
String msg;
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (AssumptionViolatedException e) {
throw e;
} catch (AssertionError e){
throw e;
} catch (Throwable t) {
msg = t.getMessage() + "; " + msg;
Throwable cause = t.getCause();
if (cause == null)
cause = t;
StackTraceElement[] stackTrace = cause.getStackTrace();
Throwable t1 = null;
try {
t1 = t.getClass().newInstance();
t1 = t.getClass().getDeclaredConstructor(String.class).newInstance(msg);
t1 = t.getClass().getDeclaredConstructor(String.class, Throwable.class).newInstance(msg, t);
t1.setStackTrace(stackTrace);
throw t1;
} catch (Throwable ignore) {
t1.setStackTrace(stackTrace);
throw t1;
}
}
}
};
}
public void setMsg(String msg) {
this.msg = msg;
}
}
And in the test case:
@Rule
public final ExceptionCatcher catcher = new ExceptionCatcher();
@Before
public void setUp() throws Exception {
catcher.setMsg("....");
}
@Test
public void testNullCheck() {
String res = someMethod();
assertThat("This is the someMethodTest", res, is(notNullValue()));
}
Upvotes: 0
Reputation: 24510
You could create an own Rule that replaces the exception:
public class NiceExceptions implements TestRule {
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (AssumptionViolatedException e) {
throw e;
} catch (Throwable t) {
throw new YourNiceException(t);
}
}
};
}
}
public class YourTest {
@Rule
public final TestRule niceExceptions = new NiceExceptions();
@Test
public void yourTest() {
...
}
}
Upvotes: 3
Reputation: 23361
What about this way:
@Test
public void testNullCheck() {
try{
String res = someMethod();
assertThat("This is the someMethodTest", res, is(notNullValue()));
}catch( Exception e /*or any especific exception*/ ){
fail("This is the someMethodTest Error " + e.getMessage() );
}
}
Upvotes: 1