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 JUni
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>
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
Agreed used method in this way in one of my junit tests and it worked
@BeforeClass public static void setupProperties() { System.setProperty("catalina.base", "C:\\sam-tomcat-7.0.42"); }
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.
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