简体   繁体   English

assertThat意外异常的错误消息

[英]assertThat error message for unexpected exception

I have this sort of JUnit test: 我有这种JUnit测试:

@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. 如果someMethod()抛出异常,我会得到一个堆栈跟踪,但是“This is the SomeMethodTest”不会被打印为assertThat()没有被调用。 Is there a somewhat elegant JUnit/hamcrest way to print a custom error message? 是否有一种优雅的JUnit / hamcrest方式来打印自定义错误消息? 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. 注意,我不想测试特定的异常。

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

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

Using Stefan Birkner's suggestion this is what I came up with. 使用Stefan Birkner的建议这就是我想出的。 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()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM