Skip to content

Instantly share code, notes, and snippets.

@adamretter
Created March 16, 2015 21:25
Show Gist options
  • Save adamretter/7b4e529b255dd521dbbf to your computer and use it in GitHub Desktop.
Save adamretter/7b4e529b255dd521dbbf to your computer and use it in GitHub Desktop.
Example if a JUnit Listener which prints the test name to the console
import org.junit.internal.JUnitSystem;
import org.junit.internal.RealSystem;
import org.junit.internal.TextListener;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import java.util.ArrayList;
import java.util.List;
/**
* Custom Junit Runner to print also Test classes
* and executed methods to command prompt.
*/
public class CustomJunitRunner {
/**
* Listener which overrides default functionality
* to print class and method to system out.
*/
static class ConsoleJunitListener extends TextListener {
public ConsoleJunitListener(JUnitSystem system) {
super(system);
}
@Override
public void testStarted(final Description description) {
System.out.format("Run: %s testing now -> %s \n",
description.getClassName(),
description.getMethodName());
}
}
public static void main(String[] args){
JUnitCore runner = new JUnitCore();
final JUnitSystem system = new RealSystem();
runner.addListener(new ConsoleJunitListener(system));
try {
List<Class<?>> classes = new ArrayList<>();
for (String arg : args) {
classes.add(Class.forName(arg));
}
runner.run(classes.toArray(new Class[1]));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment