System.out.print doesn't output to console when running from Junit
Asked Answered
T

5

22

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.

Thorp answered 22/5, 2016 at 8:36 Comment(4)
with println it works but I need it without new line though...Thorp
Why? Junit tests should not need human interaction.Trihydric
I think this has been asked and answered before here: #1119885Amnion
It works fine... I ran the same on Eclipse IDE. It is printing the dots on the same line for every 200ms. Please check on the IDE once.Tracitracie
Z
10

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.

Zelma answered 24/5, 2016 at 19:51 Comment(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...Thorp
I am also having this problem when running with antPizarro
C
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
    }};
}
Claycomb answered 16/2, 2019 at 2:43 Comment(0)
H
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.

Hexapla answered 10/8, 2021 at 9:36 Comment(0)
O
-1

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.

Organography answered 9/6, 2018 at 20:19 Comment(1)
Please exlain further. Which IDE are you talking about? I use Android Studio and these keybindings did not work for meFloatfeed
B
-1

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

Budde answered 10/3, 2023 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.