Skip to content

Commit 8b9c314

Browse files
committed
Use new scope in TestReporterParameterResolver
When injecting `TestReporter` into test class constructors the published report entries are now associated with the test method rather than the test class unless the test instance lifecycle is set to `PER_CLASS` (in which case they will continue to be associated with the test class).
1 parent 14d99b4 commit 8b9c314

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ JUnit repository on GitHub.
4444
[[release-notes-5.12.0-M1-junit-jupiter-deprecations-and-breaking-changes]]
4545
==== Deprecations and Breaking Changes
4646

47-
* ❓
47+
* When injecting `TestReporter` into test class constructors the published report entries
48+
are now associated with the test method rather than the test class unless the test
49+
instance lifecycle is set to `PER_CLASS` (in which case they will continue to be
50+
associated with the test class). If you want to publish report entries for the test
51+
class, you can implement a class-level lifecycle method (e.g., `@BeforeAll`) and inject
52+
`TestReporter` into that method.
4853

4954
[[release-notes-5.12.0-M1-junit-jupiter-new-features-and-improvements]]
5055
==== New Features and Improvements

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TestReporterParameterResolver.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
*/
2323
class TestReporterParameterResolver implements ParameterResolver {
2424

25+
@Override
26+
public ExtensionContextScope getExtensionContextScopeDuringTestClassInstanceConstruction(
27+
ExtensionContext rootContext) {
28+
return ExtensionContextScope.TEST_METHOD;
29+
}
30+
2531
@Override
2632
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
2733
return (parameterContext.getParameter().getType() == TestReporter.class);

jupiter-tests/src/test/java/org/junit/jupiter/engine/ReportingTests.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,52 @@
1212

1313
import static java.util.Collections.emptyMap;
1414
import static org.junit.jupiter.api.Assertions.assertThrows;
15+
import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME;
16+
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
17+
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;
1518

1619
import java.util.HashMap;
1720
import java.util.Map;
1821

1922
import org.junit.jupiter.api.AfterEach;
2023
import org.junit.jupiter.api.BeforeEach;
2124
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.TestInstance.Lifecycle;
2226
import org.junit.jupiter.api.TestReporter;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.CsvSource;
2329
import org.junit.platform.commons.PreconditionViolationException;
2430

2531
/**
2632
* @since 5.0
2733
*/
2834
class ReportingTests extends AbstractJupiterTestEngineTests {
2935

30-
@Test
31-
void reportEntriesArePublished() {
32-
executeTestsForClass(MyReportingTestCase.class).testEvents().assertStatistics(stats -> stats //
33-
.started(2) //
34-
.succeeded(2) //
35-
.failed(0) //
36-
.reportingEntryPublished(7));
36+
@ParameterizedTest
37+
@CsvSource(textBlock = """
38+
PER_CLASS, 7
39+
PER_METHOD, 9
40+
""")
41+
void reportEntriesArePublished(Lifecycle lifecycle, int expectedReportEntryCount) {
42+
var request = request() //
43+
.selectors(selectClass(MyReportingTestCase.class)) //
44+
.configurationParameter(DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME, lifecycle.name());
45+
executeTests(request) //
46+
.testEvents() //
47+
.assertStatistics(stats -> stats //
48+
.started(2) //
49+
.succeeded(2) //
50+
.failed(0) //
51+
.reportingEntryPublished(expectedReportEntryCount));
3752
}
3853

3954
static class MyReportingTestCase {
4055

56+
public MyReportingTestCase(TestReporter reporter) {
57+
// Reported on class-level for PER_CLASS lifecycle and on method-level for PER_METHOD lifecycle
58+
reporter.publishEntry("Constructor");
59+
}
60+
4161
@BeforeEach
4262
void beforeEach(TestReporter reporter) {
4363
reporter.publishEntry("@BeforeEach");

0 commit comments

Comments
 (0)