为从 Eclipse 运行的所有 JUnit 测试指定自定义 log4j.properties 文件 [英] Specifying a custom log4j.properties file for all of JUnit tests run from Eclipse
问题描述
我想为我从 Eclipse 运行的所有 JUnit 测试指定一个特定的 Eclipse VM 参数
,即
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
这是因为我希望所有 JUnit 测试都选择一个特定的 log4j 配置文件,而不是默认的 log4j.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.
截至目前,我必须为我的每个 JUnit 测试指定上述 VM 参数
(在 Run Configurations -> Arguments -> VM arguments
)很麻烦,因为我有很多测试.
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.
推荐答案
您无需为 JVM 指定不同的属性名称.日志代码使用类路径搜索 log4j.properties
文件.因此,您需要做的就是确保您的测试 log4j.properties
文件位于它会在发布文件之前 找到的位置.
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.
我使用 Maven,它在目录中布置文件以简化操作.我的版本 log4j.properties
位于目录 src/main/resources
中.我的测试版本在 src/test/resources
中.Eclipse 构建路径(类路径)设置为在 src/main/resources
之前搜索 src/test/resources
,因此您的单元测试使用测试文件.JAR(或 WAR)构建指令使用来自 src/main/resources
的文件.
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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!