Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 374
balteo Avatar asked Jun 15 '14 16:06

balteo


People also ask

How do you set a log4j properties file location?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.


1 Answers

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.

like image 150
Raedwald Avatar answered Sep 20 '22 16:09

Raedwald