How can I use a JUnit RunListener in Eclipse?
eclipse, junit, maven, maven-failsafe-plugin
Solution
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.
Problem
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.