tags:

views:

164

answers:

2

I have a test class that I'm trying to run from a main method with the folowing code :

Result r = org.junit.runner.JUnitCore.runClasses(TestReader.class);

when I examine the Result object I can see that 5 tests have been run but nothing is printed on the screen.

Should I do something else to get an output ?

+1  A: 

There isn't a very clean way of doing this, since it isn't a common thing to do. You only need to print the output if you are creating a program that can be used to run tests on the command line, and JUnitCore itself does that.

All of the options involve using classes in an internal package.

Result r = JUnitCore.runMain(new RealSystem(), TestReader.class.getName())

If you want to print to something other than System.out, you can create your own subclass of org.junit.internal.JUnitSystem and use that instead of RealSystem

You can also use org.junit.internal.TextListener. See runMain(JUnitSystem system, String... args) in the JUnitCore source for an example.

NamshubWriter
A: 

Yes, you should register TextListener like this:

    JUnitCore junit = new JUnitCore();
    junit.addListener(new TextListener(System.out));
    junit.run(TestReader.class);
koppernickus