为从 Eclipse 运行的所有 JUnit 测试指定自定义 log4j.properties 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24231773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:44:35  来源:igfitidea点击:

Specifying a custom log4j.properties file for all of JUnit tests run from Eclipse

eclipsejunitlog4j

提问by balteo

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

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

-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.propertiesfile.

这是因为我希望所有 JUnit 测试都选择一个特定的 log4j 配置文件,而不是默认log4j.properties文件。

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.

到目前为止,我必须为我的每个 JUnit 测试指定上述VM argument(in Run Configurations -> Arguments -> VM arguments),因为我有很多测试,这使它变得很麻烦。

回答by Raedwald

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

您不需要为 JVM 提供不同的属性名称。日志代码log4j.properties使用类路径搜索文件。因此,您需要做的就是确保您的测试log4j.properties文件位于它会发布文件之前找到的位置。

I use Maven, which lays out files in directories to make that easy. My release log4j.propertiesgoes 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/resourcesbefore src/main/resources, so your unit tests use the test file. The JAR (or WAR) build instructions use the files from src/main/resources.

我使用 Maven,它在目录中布置文件以简化操作。我的版本log4j.properties在目录中src/main/resources。我的测试版进去了src/test/resources。Eclipse 构建路径(类路径)设置为搜索src/test/resources之前src/main/resources,因此您的单元测试使用测试文件。JAR(或 WAR)构建指令使用来自src/main/resources.

回答by E-Riz

Eclipse gives you the ability to define default VM arguments that are applied to any launch which uses that VM. You could use that in your situation by defining a JRE configuration with the VM argument you want for log4j and then setting up all JUnit launches to use that JRE definition.

Eclipse 使您能够定义适用于任何使用该 VM 的启动的默认 VM 参数。您可以在您的情况下使用它,方法是使用 log4j 所需的 VM 参数定义 JRE 配置,然后设置所有 JUnit 启动以使用该 JRE 定义。

In Preferences, Java> Installed JREsand use the Add...button to define a JRE. In the JRE Definitiondialog there is a field for Default VM arguments. Give this JRE definition a useful name such as "JDK 7 for JUnit" so that you can easily identify it.

在 Preferences 中,Java> Installed JREs并使用Add...按钮定义 JRE。在JRE 定义对话框中有一个用于默认 VM 参数的字段。给这个 JRE 定义一个有用的名称,例如“JDK 7 for JUnit”,以便您可以轻松识别它。

enter image description here

在此处输入图片说明

Then in your JUnit launch(es), on the JREtab, select the JRE definition you created.

然后在您的 JUnit 启动中,在JRE选项卡上,选择您创建的 JRE 定义。

enter image description here

在此处输入图片说明