Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnitCore.runClasses doesn't print anything

Tags:

java

junit

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 ?

like image 295
Grégoire Avatar asked Apr 22 '10 11:04

Grégoire


2 Answers

Yes, you should register TextListener like this:

    JUnitCore junit = new JUnitCore();
    junit.addListener(new TextListener(System.out));
    junit.run(TestReader.class);
like image 156
kopper Avatar answered Oct 05 '22 23:10

kopper


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.

like image 28
NamshubWriter Avatar answered Oct 05 '22 23:10

NamshubWriter