Where does the output of JUnit5 TestReporter go?

Viewed 443

I'm experimenting the JUnit5 TestReporter, and even though it's so basic, I can't seem to find the output. Here is my simple test:

@Test
  void testReporterExamples(TestReporter testReporter) {
    System.out.println("Before test reporter");
    testReporter.publishEntry("My message");
    testReporter.publishEntry("key", "value");
    Map<String, String> map = new HashMap<>();
    map.put("k1", "v1");
    map.put("k2", "v2");
    testReporter.publishEntry(map);
    System.out.println("After test reporter");
  }

And here is my Gradle command and output:

% ./gradlew clean test --tests org.example.ParameterResolverTest.testReporterExamples

> Task :test

ParameterResolverTest > testReporterExamples(TestReporter) STANDARD_OUT
    Running test: testReporterExamples(TestReporter)
    Before test reporter
    After test reporter

ParameterResolverTest > testReporterExamples(TestReporter) PASSED

Test result: SUCCESS
Test summary: 1 tests, 1 succeeded, 0 failed, 0 skipped

BUILD SUCCESSFUL in 12s
5 actionable tasks: 4 executed, 1 up-to-date

As you can see, nothing is printed to stdout. I also looked at the local report file in the project: java-testing-concepts/build/reports/tests/test/classes/org.example.ParameterResolverTest.html, and the values are not published there either.

Are the values written to a log file? Thanks

1 Answers

When we are writing junit test scripts if we want to get some information out of the process we normally do System.out.println which is not recommended in corporate/enterprise world.

So in the junit world if we want to push or publish out of the scripts we are advised to use TestReporter publishEntry() method. With the combination of TestInfo we could read several information out of the original junit scripts. If not used with a logger TestReporter will only print in console output as System.out.println. I ran the exact code you provided and got this results in IntelliJ

Before test reporter
timestamp = 2021-03-02T17:22:45.331, value = My messagetimestamp = 2021-03-02T17:22:45.344, key = valuetimestamp = 2021-03-02T17:22:45.344, k1 = v1, k2 = v2After test reporter
Related