Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让JUnit4记录所有解除限制的异常?_Java_Unit Testing_Logging - Fatal编程技术网

Java 如何让JUnit4记录所有解除限制的异常?

Java 如何让JUnit4记录所有解除限制的异常?,java,unit-testing,logging,Java,Unit Testing,Logging,嘿,我正在使用SpringJUnit4ClassRunner,并使用EclipseUI运行大型测试套件。 当测试失败时,我可以在junit视图中看到异常和完整的堆栈跟踪。我希望它们也被记录下来。 (使用apache.commons.logging包装的log4j)如果您使用maven进行构建,那么它附带了一个sure fire插件,该插件提供测试用例的详细报告。在eclipse中,您可以将应用程序控制台记录下来。我相信它在应用程序的runconfig中。虽然我不知道确切的细节。如果您将log4j

嘿,我正在使用SpringJUnit4ClassRunner,并使用EclipseUI运行大型测试套件。 当测试失败时,我可以在junit视图中看到异常和完整的堆栈跟踪。我希望它们也被记录下来。

(使用apache.commons.logging包装的log4j)

如果您使用maven进行构建,那么它附带了一个sure fire插件,该插件提供测试用例的详细报告。在eclipse中,您可以将应用程序控制台记录下来。我相信它在应用程序的runconfig中。虽然我不知道确切的细节。

如果您将log4j添加到您的项目中,您可以通过以下方式记录异常:

testclass中的初始化记录器:

 private static final Logger logger = Logger.getLogger(YourClassNameclass);
下面是记录消息的代码:

try{
 your code here...
} catch (SomeException e){
logger.error("Description of your error", e);
}
下面是log4j.properties中用于将消息记录到文件中的模式:

log4j.rootLogger=ALL,A1

log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=Testlogfile.log
log4j.appender.A1.append=false
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=\
%-4r [%t] %-5p %c %x -%m%n

祝你好运!:)

扩展SpringJUnit4ClassRunner:

public class MySpringJUnit4ClassRunner extends SpringJUnit4ClassRunner{
    @Override
    public void run(RunNotifier notifier) {
    EachTestNotifier testNotifier= new EachTestNotifier(notifier,
            getDescription());
    try {
        Statement statement= classBlock(notifier);
        statement.evaluate();
    } catch (AssumptionViolatedException e) {
        testNotifier.fireTestIgnored();
        Logger.error(e);
    } catch (StoppedByUserException e) {
        Logger.error(e);
        throw e;
    } catch (Throwable e) {
        testNotifier.addFailure(e);
        Logger.error(e);
    }
  }
 //...
}

然后使用MySpringJUnit4ClassRunner作为您的跑步者。

同意。JUnit不提供日志模块,您必须自己编写日志代码。