本文介绍了为从Eclipse运行的所有JUnit测试指定自定义log4j.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Eclipse中运行的所有JUnit测试指定一个特定的Eclipse VM参数,即

  -Dlog4j.configuration = log4j-dev.properties 

这是因为我想要一个特定的log4j配置文件被所有的JUnit测试代替,而不是默认的 log4j.properties 文件。



到目前为止,我必须指定上述 VM参数(在运行配置 - >参数 - > VM参数)对于我的每个JUnit测试,使其繁琐,因为我有很多测试。

解决方案

你不需要给JVM一个不同的属性名。日志记录代码使用类路径搜索 log4j.properties 文件。所以所有你需要做的是确保你的测试 log4j.properties 文件位于它将在发布文件之前找到的位置。 p>

我使用Maven,它将文件放在目录中,使其变得简单。我的发行版 log4j.properties 进入目录 src / main / resources 。我的测试版本在 src / test / resources 中。 Eclipse构建路径(classpath)设置为在 src / main / resources 之前搜索 src / test / resources 所以你的单元测试使用测试文件。 JAR(或WAR)构建指令使用 src / main / resources 中的文件。


I would like to specify a specific Eclipse VM argument to all of the JUnit tests I run from Eclipse i.e.

-Dlog4j.configuration=log4j-dev.properties

This is because I want a specific log4j configuration file to be picked up by all my JUnit tests instead of the default log4j.properties file.

As of now I have to specify the above VM argument (in Run Configurations -> Arguments -> VM arguments) for each of my JUnit tests making it cumbersome because I have many tests.

解决方案

You do not need to give the JVM a different property name. The logging code searches for the log4j.properties file using the classpath. So all you need to do is ensure that your test log4j.properties file is in a location that it will find before the release file.

I use Maven, which lays out files in directories to make that easy. My release log4j.properties goes in the directory src/main/resources. My test version goes in src/test/resources. The Eclipse build path (classpath) is set up to search src/test/resources before src/main/resources, so your unit tests use the test file. The JAR (or WAR) build instructions use the files from src/main/resources.

这篇关于为从Eclipse运行的所有JUnit测试指定自定义log4j.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:43