-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix support for package-private test methods #5100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b2c6c30
Fix support for package-private test methods
marcphilipp fa4b78a
Merge branch 'main' into marc/5098-package-private-hidden-methods
marcphilipp 7b67a8c
Avoid depending on mutable fields in `MethodSelector.equals()`
marcphilipp aa4a84a
Put segment formatting and parsing into `MethodSegmentResolver`
marcphilipp 31128d6
Ignore declaring class when equal to class name
mpkorstanje c6999dc
Remove `declaringClassName` from `MethodSelector`
marcphilipp 1928f36
Introduce Jupiter-specific `DeclaredMethodSelector`
marcphilipp 8d22e78
Document `DeclaredMethodSelector`
marcphilipp cc0ce23
Check that `MethodSource` preserves `Method`
marcphilipp 94ba3f1
Merge branch 'main' into marc/5098-package-private-hidden-methods
marcphilipp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/MethodFinder.java
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
...upiter-engine/src/main/java/org/junit/jupiter/engine/discovery/MethodSegmentResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright 2015-2025 the original author or authors. | ||
| * | ||
| * All rights reserved. This program and the accompanying materials are | ||
| * made available under the terms of the Eclipse Public License v2.0 which | ||
| * accompanies this distribution and is available at | ||
| * | ||
| * https://www.eclipse.org/legal/epl-v20.html | ||
| */ | ||
|
|
||
| package org.junit.jupiter.engine.discovery; | ||
|
|
||
| import static org.junit.platform.commons.util.ReflectionUtils.isPackagePrivate; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.Optional; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.junit.platform.commons.PreconditionViolationException; | ||
| import org.junit.platform.commons.support.ReflectionSupport; | ||
| import org.junit.platform.commons.util.ClassUtils; | ||
| import org.junit.platform.commons.util.Preconditions; | ||
| import org.junit.platform.commons.util.ReflectionUtils; | ||
|
|
||
| /** | ||
| * @since 5.0 | ||
| */ | ||
| class MethodSegmentResolver { | ||
|
|
||
| // Pattern: [declaringClassName#]methodName(comma-separated list of parameter type names) | ||
| private static final Pattern METHOD_PATTERN = Pattern.compile( | ||
| "(?:(?<declaringClass>.+)#)?(?<method>.+)\\((?<parameters>.*)\\)"); | ||
|
|
||
| /** | ||
| * If the {@code method} is package-private and declared a class in a | ||
| * different package than {@code testClass}, the declaring class name is | ||
| * included in the method's unique ID segment. Otherwise, it only | ||
| * consists of the method name and its parameter types. | ||
| */ | ||
| String formatMethodSpecPart(Method method, Class<?> testClass) { | ||
| var parameterTypes = ClassUtils.nullSafeToString(method.getParameterTypes()); | ||
| if (isPackagePrivate(method) | ||
| && !method.getDeclaringClass().getPackageName().equals(testClass.getPackageName())) { | ||
| return "%s#%s(%s)".formatted(method.getDeclaringClass().getName(), method.getName(), parameterTypes); | ||
| } | ||
| return "%s(%s)".formatted(method.getName(), parameterTypes); | ||
| } | ||
|
|
||
| Optional<Method> findMethod(String methodSpecPart, Class<?> testClass) { | ||
| Matcher matcher = METHOD_PATTERN.matcher(methodSpecPart); | ||
|
|
||
| Preconditions.condition(matcher.matches(), | ||
| () -> "Method [%s] does not match pattern [%s]".formatted(methodSpecPart, METHOD_PATTERN)); | ||
|
|
||
| Class<?> targetClass = testClass; | ||
| String declaringClass = matcher.group("declaringClass"); | ||
| if (declaringClass != null) { | ||
| targetClass = ReflectionUtils.tryToLoadClass(declaringClass).getNonNullOrThrow( | ||
| cause -> new PreconditionViolationException( | ||
| "Could not load declaring class with name: " + declaringClass, cause)); | ||
| } | ||
| String methodName = matcher.group("method"); | ||
| String parameterTypeNames = matcher.group("parameters"); | ||
| return ReflectionSupport.findMethod(targetClass, methodName, parameterTypeNames); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
jupiter-tests/src/test/java/org/junit/jupiter/engine/TestMethodOverridingTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright 2015-2025 the original author or authors. | ||
| * | ||
| * All rights reserved. This program and the accompanying materials are | ||
| * made available under the terms of the Eclipse Public License v2.0 which | ||
| * accompanies this distribution and is available at | ||
| * | ||
| * https://www.eclipse.org/legal/epl-v20.html | ||
| */ | ||
|
|
||
| package org.junit.jupiter.engine; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.platform.commons.util.CollectionUtils.getOnlyElement; | ||
| import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestInfo; | ||
| import org.junit.jupiter.api.TestReporter; | ||
| import org.junit.jupiter.engine.subpackage.SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase; | ||
| import org.junit.platform.engine.TestDescriptor; | ||
| import org.junit.platform.engine.reporting.ReportEntry; | ||
| import org.junit.platform.testkit.engine.EngineExecutionResults; | ||
|
|
||
| /** | ||
| * @since 6.0.1 | ||
| */ | ||
| class TestMethodOverridingTests extends AbstractJupiterTestEngineTests { | ||
|
|
||
| @Test | ||
| void bothPackagePrivateTestMethodsAreDiscovered() { | ||
| var results = discoverTestsForClass(DuplicateTestMethodDueToPackagePrivateVisibilityTestCase.class); | ||
| var classDescriptor = getOnlyElement(results.getEngineDescriptor().getChildren()); | ||
|
|
||
| assertThat(classDescriptor.getChildren()).hasSize(2); | ||
|
|
||
| var parentUniqueId = classDescriptor.getUniqueId(); | ||
| var inheritedMethodUniqueId = parentUniqueId.append("method", | ||
| "org.junit.jupiter.engine.subpackage.SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase#" | ||
| + "test(org.junit.jupiter.api.TestInfo, org.junit.jupiter.api.TestReporter)"); | ||
| var declaredMethodUniqueId = parentUniqueId.append("method", | ||
| "test(org.junit.jupiter.api.TestInfo, org.junit.jupiter.api.TestReporter)"); | ||
|
|
||
| assertThat(classDescriptor.getChildren()) // | ||
| .extracting(TestDescriptor::getUniqueId) // | ||
| .containsExactly(inheritedMethodUniqueId, declaredMethodUniqueId); | ||
|
|
||
| results = discoverTests(selectUniqueId(inheritedMethodUniqueId)); | ||
| classDescriptor = getOnlyElement(results.getEngineDescriptor().getChildren()); | ||
| assertThat(classDescriptor.getChildren()) // | ||
| .extracting(TestDescriptor::getUniqueId) // | ||
| .containsExactly(inheritedMethodUniqueId); | ||
|
|
||
| results = discoverTests(selectUniqueId(declaredMethodUniqueId)); | ||
| classDescriptor = getOnlyElement(results.getEngineDescriptor().getChildren()); | ||
| assertThat(classDescriptor.getChildren()) // | ||
| .extracting(TestDescriptor::getUniqueId) // | ||
| .containsExactly(declaredMethodUniqueId); | ||
| } | ||
|
|
||
| @Test | ||
| void bothPackagePrivateTestMethodsAreExecuted() throws Exception { | ||
| var results = executeTestsForClass(DuplicateTestMethodDueToPackagePrivateVisibilityTestCase.class); | ||
|
|
||
| results.testEvents().assertStatistics(stats -> stats.started(2).succeeded(2)); | ||
| assertThat(allReportEntries(results)) // | ||
| .containsExactly( | ||
| Map.of("invokedSuper", | ||
| SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase.class.getDeclaredMethod( | ||
| "test", TestInfo.class, TestReporter.class).toGenericString()), | ||
| Map.of("invokedChild", | ||
| DuplicateTestMethodDueToPackagePrivateVisibilityTestCase.class.getDeclaredMethod("test", | ||
| TestInfo.class, TestReporter.class).toGenericString())); | ||
| } | ||
|
|
||
| private static Stream<Map<String, String>> allReportEntries(EngineExecutionResults results) { | ||
| return results.allEvents().reportingEntryPublished() // | ||
| .map(event -> event.getRequiredPayload(ReportEntry.class)) // | ||
| .map(ReportEntry::getKeyValuePairs); | ||
| } | ||
|
|
||
| @SuppressWarnings("JUnitMalformedDeclaration") | ||
| static class DuplicateTestMethodDueToPackagePrivateVisibilityTestCase | ||
| extends SuperClassWithPackagePrivateLifecycleMethodInDifferentPackageTestCase { | ||
|
|
||
| // @Override | ||
| @Test | ||
| void test(TestInfo testInfo, TestReporter reporter) { | ||
| reporter.publishEntry("invokedChild", testInfo.getTestMethod().orElseThrow().toGenericString()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.