Skip to content

Commit

Permalink
Use new scope in TestReporterParameterResolver
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
marcphilipp committed Oct 10, 2024
1 parent 14d99b4 commit 8b9c314
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ JUnit repository on GitHub.
[[release-notes-5.12.0-M1-junit-jupiter-deprecations-and-breaking-changes]]
==== Deprecations and Breaking Changes

* ❓
* 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). If you want to publish report entries for the test
class, you can implement a class-level lifecycle method (e.g., `@BeforeAll`) and inject
`TestReporter` into that method.

[[release-notes-5.12.0-M1-junit-jupiter-new-features-and-improvements]]
==== New Features and Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
*/
class TestReporterParameterResolver implements ParameterResolver {

@Override
public ExtensionContextScope getExtensionContextScopeDuringTestClassInstanceConstruction(
ExtensionContext rootContext) {
return ExtensionContextScope.TEST_METHOD;
}

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return (parameterContext.getParameter().getType() == TestReporter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,52 @@

import static java.util.Collections.emptyMap;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.platform.commons.PreconditionViolationException;

/**
* @since 5.0
*/
class ReportingTests extends AbstractJupiterTestEngineTests {

@Test
void reportEntriesArePublished() {
executeTestsForClass(MyReportingTestCase.class).testEvents().assertStatistics(stats -> stats //
.started(2) //
.succeeded(2) //
.failed(0) //
.reportingEntryPublished(7));
@ParameterizedTest
@CsvSource(textBlock = """
PER_CLASS, 7
PER_METHOD, 9
""")
void reportEntriesArePublished(Lifecycle lifecycle, int expectedReportEntryCount) {
var request = request() //
.selectors(selectClass(MyReportingTestCase.class)) //
.configurationParameter(DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME, lifecycle.name());
executeTests(request) //
.testEvents() //
.assertStatistics(stats -> stats //
.started(2) //
.succeeded(2) //
.failed(0) //
.reportingEntryPublished(expectedReportEntryCount));
}

static class MyReportingTestCase {

public MyReportingTestCase(TestReporter reporter) {
// Reported on class-level for PER_CLASS lifecycle and on method-level for PER_METHOD lifecycle
reporter.publishEntry("Constructor");
}

@BeforeEach
void beforeEach(TestReporter reporter) {
reporter.publishEntry("@BeforeEach");
Expand Down

0 comments on commit 8b9c314

Please sign in to comment.