n00b programmer n00b programmer - 1 year ago 152
Java Question

Junit test to fail without failure trace

I'm running JUnit tests from Eclipse, and using the command

whenever I want to cause the test to fail. The problem is that I always get the message along with the entire failure trace. Is there any way for JUnit to display the message only, without the stack trace?

Answer Source

I found a way to crash the test without showing a long failure trace. Instead of using Assert.fail(), I create my own exception, and set the failure trace to be minimal:

public static void failTest(String message) throws MyTestFailure {
    MyTestFailure exception = new MyTestFailure(message);
    StackTraceElement elem = new StackTraceElement("com.my.package.Utils", "failTest", "failTest", 3);
    StackTraceElement elem1[] = new StackTraceElement[1];
    elem1[0] = elem;
    throw exception;

and this way I only print the message that I want to print, and a single line after that

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download