balteo
balteo

Reputation: 24709

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.

Upvotes: 16

Views: 23770

Answers (2)

E-Riz
E-Riz

Reputation: 33034

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.

In Preferences, Java > Installed JREs and use the Add... button to define a JRE. In the JRE Definition dialog 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.

enter image description here

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

enter image description here

Upvotes: 3

Raedwald
Raedwald

Reputation: 48712

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.

Upvotes: 41

Related Questions