Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

People also ask

How do I run JUnit in debug mode?

In the case of a test failure you can follow these steps to debug it: Double click the failure entry from the Failures tab in the JUnit view to open the corresponding file in the editor. Set a breakpoint at the beginning of the test method. Select the test case and execute Debug As>JUnit Test from the Debug drop down.

How do I get JUnit to work in Eclipse?

To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.


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.