I have a set of tests that need a database to be executed. I want to create the database at the beginning of their execution and remove it at the end.
From maven I've also added a RunListener to the maven-surefire-plugin and it works fine. And I've also added a system property variable named ismaven. When I execute the test from maven this variable is initialized but when I execute the tests from the Eclipse, this variable is null (I access to the variable with System.getProperty).
<configuration>
<properties>
<property>
<name>listener</name>
<value>com.mycompany.MyRunListener</value>
</property>
</properties>
<systemPropertyVariables>
<ismaven>true</ismaven>
</systemPropertyVariables>
</configuration>
All my database tests inherit from a class that has a @BeforeClass and an @AfterClass methods. These methods check if the test is being executed by Maven or by the Eclipse checking the value of the ismaven property. If the test is being executed by maven, the ismaven property has a value and they do anything. But is the test is being executed by the Eclipse, the ismaven variable is null and they starts (@BeforeClass) or stops (@AfterClass) the database:
@BeforeClass
public static void checkIfStartDatabase() {
String ismaven = System.getProperty("ismaven");
// If it is not maven, start the database
if (ismaven == null) {
startDatabase();
}
}
@AfterClass
public static void checkIfStopDatabase() {
String ismaven = System.getProperty("ismaven");
// If it is not maven, stop the database
if (ismaven == null) {
stopDatabase();
}
}
This solution doesn't solve 100% your problem but if you implement it you can execute (and debug) all the tests of one JUnit class using the Eclipse and you can also execute all the tests of your project using Maven with the guarantee that you will execute once a piece of code before or after the execution of all your tests.