18

When running:

public static void main(String... args) throws InterruptedException {
    while (true) {
        System.out.print(".");
        Thread.sleep(200);
    }
}

vs. when running same code from junit:

@Test
public void test() throws Exception {
    while (true) {
        System.out.print(".");
        Thread.sleep(200);
    }
}

There is different behaviour:
for the main() - the output is displayed as expected, as the process runs ("." -> ".." -> "...")

however, for JUnit, when running same piece of code, No output is displayed as the process runs - It's flushed only when quitting the test.

Why does this happen? Is there any way to workaround it if I need the status to be shown in console during the test run?

I need to print to the same line, so using println wouldn't suit.

Lika
  • 1,043
  • 2
  • 10
  • 13

5 Answers5

9

There are many ways people run JUnit tests (from within an IDE, from within a build system like Maven, or from a command line that uses the JUnit library directly). It appears that the way that you're running it uses a standard output that doesn't flush on every output. (This is probably intentional, as often tests are run in a batch using a continuous integration system, and the logs reviewed afterward, so not flushing on every write can improve performance.)

But, if you need to flush the buffer explicitly, try using System.out.flush(); after each .print call.

Another option, depending on what you're actually looking to do, may be to use a more full-featured logging system than the built-in System.out stream.

  • 2
    Thanks for the explanation, that seems to be the reason, but explicit use of System.out.flush(); after each .print(".") doesn't display the output either (when running from within intelliJ IDE). println(".") on the other hand does display the output... – Lika May 29 '16 at 09:59
  • I am also having this problem when running with ant – Bikash Gyawali Aug 25 '17 at 12:15
0

Ctrl + Shift + p and type show test output. Alternatively you can open the output window (Ctrl + J) and choose to see Test Output from a combo box on it's right upper corner.

0

This still doesn't work in IntelliJ 2019. *sigh*

@Test
public void test() throws Exception {
    while (true) {
        System.out.print(".");
        Thread.sleep(200);
    }
}

To achieve somewhat what you are looking for I had to force a newline periodically like this:

@Before
public void forceConsoleOutput()
{
    new Thread( () -> {
        for ( ; ; )
        {
            // Does nothing. Why JetBrains?
            // System.out.flush();

            System.out.println();
            try
            {
                Thread.sleep( 5000 );
            }
            catch ( InterruptedException e )
            {
                e.printStackTrace();
            }
        }
    } )
    {{
        start();
        System.out.println( "Terminated" ); // The threads naturally stop between method tests
    }};
}
Drakes
  • 23,254
  • 3
  • 51
  • 94
0

Alternatively, you can use a logger of your choice. Used log4j to printout on console. Import below dependence on pom.xml

  <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

In the JUnitTestClass import

import org.apache.log4j.Logger;

Then declare log

public static Logger log = Logger.getLogger(JUnitTest.class.getName());

to printout log.info("....");

Hope this works for your case.

Duncan O. N.
  • 778
  • 12
  • 24
0

Create your logs using System.out.println() instead. I know that's not the best solution but worked for me.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36