【问题标题】:How to make maven not show warnings when running jUnit tests which expects exception?如何在运行预期异常的 jUnit 测试时使 maven 不显示警告?
【发布时间】:2011-06-09 10:52:09
【问题描述】:

当 maven 运行 jUnit 测试时,期望 exption 这个异常的堆栈跟踪记录并显示在输出中。有没有办法关闭此日志记录。这很麻烦,因为构建运行良好但输出中有堆栈跟踪。

【问题讨论】:

    标签: exception logging maven junit


    【解决方案1】:

    Maven Surefire 插件 2.7.2 不这样做。您使用的是 JUnit 3 还是 4?

    这样的测试不会打印任何堆栈跟踪,无论是在 maven 输出中还是在报告文件中:

    @Test(expected = NumberFormatException.class)
    public void testForExpectedExceptionWithAnnotation()
            throws Exception {
        Integer.parseInt("This should blow up...");
    }
    

    在 jUnit 3 中,这样的测试也不会打印堆栈跟踪。你的测试是这样的吗?:

    public testForExpectedException() throws Exception{
      try{
        Integer.parseInt("This should blow up...");
     }catch(NumberFormatException e){
         //Just ignore it or do some asserts you could need
     }  
    
     fail("Exceptions not thrown");
    }
    

    【讨论】:

      【解决方案2】:

      您应该更改测试方法不打印堆栈跟踪,因为 Surefire 运行器仅显示运行测试用例返回的内容。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,为了解决它,我在单元测试使用的 log4j.properties 中添加了以下行:

        log4j.additivity.org.hibernate=false  
        

        【讨论】:

          猜你喜欢
          • 2012-08-07
          • 1970-01-01
          • 1970-01-01
          • 2012-07-08
          • 2011-03-22
          • 1970-01-01
          • 1970-01-01
          • 2011-05-28
          • 1970-01-01
          相关资源
          最近更新 更多