views:

2762

answers:

4

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

+4  A: 

My solution is to create an abstract test base class for all tests in a project which extends TestCase. It has to be abstract to 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.

Aaron Digulla
I can't use your idea, as explained in my Edit (I don't downvote it as it is not a bad idea, however)...
romaintaz
+3  A: 

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

Markus Lausberg
Same remark as for Aaron Digulla answer. It will not work if the properties are used by Spring...
romaintaz
You should initialize all values and objects inside the setup method. Otherwise update the testcases.
Markus Lausberg
A: 

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>
chillitom
+1  A: 

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
siddhadev