Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a JUnit RunListener in Eclipse?

I wrote a simple RunListener for JUnit which works quite well with Maven. I could register it for the maven-failsafe-plugin via

<properties>
    <property>
        <name>listener</name>
        <value>com.asml.lcp.middleware.common.jboss.test.tps.TestDocumentationListener</value>
    </property>
</properties>

and see the correct output from the listener.

Now I want to register the same RunListener in Eclipse to see the same output there, when I run the tests.

Is this possible? For testing purposes and to be consistent it would be nice to have the same output.

like image 370
Rick-Rainer Ludwig Avatar asked May 10 '12 15:05

Rick-Rainer Ludwig


1 Answers

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.

like image 62
Jorge Piera Llodrá Avatar answered Sep 27 '22 23:09

Jorge Piera Llodrá