Testing / JUnit6 Interview Questions
What is TestReporter in JUnit 6 and how do you use it?
TestReporter lets test methods publish key-value entries to the test execution report. Unlike System.out.println, reported values are attached to the test result and appear in IDE test reports and XML output -- they are not just lost in console noise.
import org.junit.jupiter.api.TestReporter; class TestReporterDemo { @Test void reportingDemo(TestReporter reporter) { // Publish a single key-value entry reporter.publishEntry("user-id", "U-12345"); reporter.publishEntry("status", "active"); // Publish a map of entries in one call Map<String, String> values = Map.of( "order-id", "O-99", "total", "149.99", "currency", "GBP" ); reporter.publishEntry(values); // Assertions still run after publishing entries assertTrue(true); } @Test void reportWithMessage(TestReporter reporter) { // Single-argument form: just a message string reporter.publishEntry("Processing payment for order O-42"); } } // TestReporter vs System.out: // // System.out.println: // - Goes to process stdout // - Not associated with any specific test in the report // - Lost in CI log noise // // TestReporter.publishEntry: // - Attached to the specific test result // - Appears in IDE test runner output alongside the test // - Included in XML/HTML test reports // - Visible when you click on a specific test result in the IDE
More Related questions...