assertThat意外异常的错误消息

时间:2013-12-11 18:59:41

标签: java junit

我有这种JUnit测试:

@Test
public void testNullCheck() {
   String res = someMethod();
   assertThat("This is the someMethodTest", res, is(notNullValue()));
}

如果someMethod()抛出异常,我会得到一个堆栈跟踪,但不打印“This is someMethodTest”,因为assertThat()未被调用。是否有一种优雅的JUnit / hamcrest方式来打印自定义错误消息?最后,我希望在参数化测试中打印测试失败的参数。注意,我不想测试特定的异常。

3 个答案:

答案 0 :(得分:3)

您可以创建一个替换异常的自己的Rule

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() {
    ...
  }
}

答案 1 :(得分:1)

这样:

@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() );
   }
} 

答案 2 :(得分:0)

使用Stefan Birkner的建议这就是我想出的。欢迎评论。

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;
    }

}

在测试案例中:

@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()));
}