Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically add properties when running JUnit in Eclipse

In order to run my unit tests on my Eclipse, I need to set some properties for the VM.

Thus, when I first run my JUnit test, I go in "Open Run Dialog", then in my JUnit configuration for this test, I go in "Arguments" tab and put everything I need in the "VM arguments" text area.

Is there a way to automatically add a set of properties when I run my JUnit, so I will be able to only right-click on the test class, and click on "Run as > Junit Test" to run a test?

Technical information: Eclipse 3.3.2, JUnit 4, Java 5


Edit, regarding response from Aaron Digulla:

These properties are used in Spring configuration files*. Thus, I can't use the idea given by Aaron, as Spring will be initialized before the test is run.

In addition to that, I just need to know if I can achieve that in a easy way in Eclipse. Thus the solution must not have any impact on the compilation of the application outside Eclipse, as my application will finally be compiled (and tested) by Maven2.

* few "unit" tests indeed need my Spring configuration to be run. Ok, I know that it is not real unit tests ;o)

Edit 2: In fact, I was indeed starting the Spring configuration by a test unit. Thus, before starting Spring, I check the System properties, and if my properties are not set, then I give them the required value...

However, I am a little disappointed that Eclipse can't do that for me automatically...

like image 872
Romain Linsolas Avatar asked Mar 11 '09 08:03

Romain Linsolas


People also ask

How do you pass system properties in JUnit?

If your test relies on system properties you could set them and unset them in 'before' and 'after' lifecycle methods. In Junit5, setting system properties for all tests in a test case might look like this: @BeforeAll public static void setSystemProperties() { // set the system properties // ... }

How do I change JUnit settings in Eclipse?

Open eclipse → right click on project and click on property > Build Path > Configure Build Path and add the junit-4.10. jar in the libraries using the button Add External Jar. We assume that your Eclipse has inbuilt JUnit plugin.

How do I run JUnit tests automatically?

Running Single Test Class. To run JUnit 5 tests from Java code, we'll set up an instance of LauncherDiscoveryRequest. It uses a builder class where we must set package selectors and testing class name filters, to get all test classes that we want to run.

Can JUnit run automatically?

The JUnit framework automatically invokes any @Before methods before each test is run. The following example shows a test fixture with a common Collection object.


4 Answers

You could try this - go to

 Window->Preferences->Java->Installed JREs

ans select the JVM in use, than put a "Default VM" prameter like

  -DrunningInEclipse

Than you can check from within your TestCase:

   System.getProperty("runningInEclipse") != null
like image 116
siddhadev Avatar answered Oct 23 '22 05:10

siddhadev


My solution is to create an abstract test base class for all tests in a project which extends TestCase. It has to be abstract so the automatic unit test finder will not consider it.

In static code block of this class, I set all properties I need. This ensures that the code runs once and only once and that it runs before any test in my project.

[EDIT] You say that Spring is initialized before the tests run. This is a bug in your project: It must be the tests who initialize Spring. Otherwise, you will always run into the problem that you have to test something outside of your control.

Therefore, I suggest to move the Spring init code into a place where you can call it at the point in time when the environment is ready.

Alternatively, check that the environment is correctly set up in setUp() and throw an error if a property is missing. This way, you will at least know why the tests would fail later. But I still prefer to have total control when which subsystem comes to life. Anything else just begs for disaster.

like image 22
Aaron Digulla Avatar answered Oct 23 '22 06:10

Aaron Digulla


When i want to set some properties entries for my junit test i implement the following

protected void setUp() throws Exception {
        super.setUp();

        System.setProperty("Property1", "value1");
        System.setProperty("Property2", "value2");
}

The properties are set before the test methode is called

EDIT: You also can read the properties from a file and at thes to the System properties

like image 20
Markus Lausberg Avatar answered Oct 23 '22 04:10

Markus Lausberg


I never understood why the launch configurations have a way to define environment variables but the only way of adding a system property seems to be to add vm arguments.

The way I've worked around this is to have tests (or an abstract tests base class) test for the presence of required properties, if they aren't there then I load them from a .properties file on the classpath.

This works as I can still override them or specify them from ANT or Maven but can also 'right click' -> Run As -> Junit Test the individual test files.

edit: here is an example of getting Spring to optionally load a properties file in the same manner as described above:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="database.properties"/>
        <property name="ignoreResourceNotFound" value="true" />
        <property name="systemPropertiesMode">
            <util:constant static-field="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        </property>
</bean>
like image 29
chillitom Avatar answered Oct 23 '22 04:10

chillitom