How can I use a JUnit RunListener in Eclipse?

青春壹個敷衍的年華 提交于 2019-12-05 05:19:31
John Henckel

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.

Jorge Piera Llodrá

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.

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.

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