1

Section 4.7 of the JUnit user manual (https://junit.org/junit5/docs/current/user-guide/#running-tests-capturing-output) talks about capturing standard output/error using listeners, and the following section says that the API already contains a class called LoggingListener. I can create one using:

LoggingListener listener = LoggingListener.forJavaUtilLogging(Level.INFO);

but I'm not sure how to proceed after that. I want to capture the log output happening at INFO level. I already added

junit.platform.output.capture.stdout=true
junit.platform.output.capture.stderr=true

to junit-platform.properties under src/test/resources, as it says to do. The listener class contains a method called reportingEntryPublished which takes a TestIdentifier and a ReportEntry, but I'm not sure how to use that, assuming that's even the right method to call.

Any advice would be appreciated. The User Manual section makes it sound easy, but I can't find any examples anywhere (including in the tests of the source code on GitHub).

BTW, suggestions from both ChatGPT and Bard are hilarious.

kousen
  • 2,897
  • 3
  • 28
  • 26
  • I haven't used the LoggingListener but I've had success with the OutputCapture, although I've run into issues with timing... Check this out, might help https://stackoverflow.com/questions/24439151/how-to-unit-test-logging-error-with-spock-framework-in-groovy/61268662#comment134910935_61268662 – Tocchetto Jun 20 '23 at 14:19

0 Answers0