1

I am using Java util logging (JUL) in a Junit test class with JUL working in default config (i.e. printing to console).

Logs statements from methods annotated with @BeforeClass and @AfterClass are getting printed when they are executed but logs in '@Test' methods are printed only after all test execution is finished.

Not sure what exactly is wrong because the same were working earlier.

Logger instantiation:

private static Logger logger = Logger.getLogger(MainIntegrationTest.class.getName());

Logger use:

logger.info("start test");

The test cases are run using maven. I noticed that this started happening only after I started running the test classes in parallel using this surefire configuration:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
        <parallel>classes</parallel>
        <useUnlimitedThreads>true</useUnlimitedThreads>
    </configuration>
</plugin>

The logs are printed at test execution time if I run the test classes in serial fashion.

Akhil Raina
  • 379
  • 2
  • 9
  • If your test logs something then calls `Assert.fail()` does the message get logged? – NamshubWriter Jul 06 '17 at 08:07
  • Actually, the logs from test methods are printed but only after all tests have finished. Have edited the question to give more info. – Akhil Raina Jul 13 '17 at 11:57
  • Related: [Running test in parallel with surefire and displaying them properly with the TestNG Jenkins plugin](https://stackoverflow.com/questions/28883046/running-test-in-parallel-with-surefire-and-displaying-them-properly-with-the-tes) – jmehrens Jul 14 '17 at 12:33

1 Answers1

1

The test cases are run using maven. I noticed that this started happening only after I started running the test classes in parallel using this surefire configuration

The output would become scrambled if all tests were writing to console concurrently. For concurrent execution the results have to be buffered for the whole run and then written to the console as one chunk. The only safe time to write to the console is when the test is complete.

Even if the buffering was performed line by line this could lead to interleaved messages in the console which could be hard to read or even misdirect you as you don't know the source of the line.

jmehrens
  • 10,580
  • 1
  • 38
  • 47