How can I use a JUnit RunListener in Eclipse?

只愿长相守 提交于 2019-12-07 00:44:09

问题


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.


回答1:


Yes, it is possible. Basically you have to implement your own Runner and inside the run method, you can add a custom run listener. I figured this out based on this post, point 2.

Here is my listener

public class TestLogger extends RunListener
{
    public void testFinished(Description description)
    {
        System.out.println("Successful " + description.getMethodName());
    }
}

and here is my Runner

public class TestRunner extends BlockJUnit4ClassRunner
{
    public TestRunner(Class<?> klass) throws InitializationError
    {
        super(klass);
    }

    @Override
    public void run(RunNotifier notifier)
    {
        notifier.addListener(new TestLogger());   // THIS IS THE IMPORTANT LINE
        super.run(notifier);
    }
}

and here is my actual junit test

@RunWith(TestRunner.class)           // THIS LINE IS ALSO IMPORTANT
public class MyTest1
{
    @Test
    public void Test1() throws Exception
    {
        if (Math.random() < .5) throw new Exception("ouch");
        assertFalse(Math.random() < .5);
    }
}    

You can run MyTest1 or the Test1 method using the context menu in Eclipse and it will invoke the testLogger as you would expect.




回答2:


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.




回答3:


I did some more reseatrch on this. As far as I can see now, there is no way to add a run listener too the JUnit runs in Eclipse.



来源:https://stackoverflow.com/questions/10537495/how-can-i-use-a-junit-runlistener-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!