-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
@BeforeAll
method in super class skipped when it has same name as a @BeforeEach
method in subclass
#3498
Comments
@BeforeAll
method in super class skipped when it has same name as a @BeforeEach
method in subclass
Thanks for raising the issue. I have confirmed that this is a bug. We'll look into it. |
At a quick glance, the cause of the issue appears to be due to the fact that Whereas, for such use cases, we would need to apply the predicate while searching the hierarchy for methods. If we did that, |
This is a proof-of-concept fix for junit-team#3498.
Update: I put together a proof-of-concept fix in #3500. |
Prior to this commit, findMethods() and streamMethods() in ReflectionSupport as well as findAnnotatedMethods() in AnnotationSupport first searched for all methods in the type hierarchy and then applied the user-supplied predicate (or "is annotated?" predicate) afterwards. This resulted in methods in subclasses incorrectly "shadowing" package-private methods in superclasses (in a different package) even if the predicate would otherwise exclude the method in such a subclass. For example, given a superclass that declares a package-private static @BeforeAll "before()" method and a subclass (in a different package) that declares a @BeforeEach "before()" method, when JUnit Jupiter looked up @BeforeAll methods for the subclass, the @BeforeAll "before()" method in the superclass was not found because the @BeforeEach "before()" method shadowed it based solely on the method signature, ignoring the type of annotation sought. To address that, this commit modifies the internal search algorithms in ReflectionUtils so that method predicates are applied while searching the hierarchy for methods. Closes junit-team#3498 Closes junit-team#3500
Prior to this commit, findMethods() and streamMethods() in ReflectionSupport as well as findAnnotatedMethods() in AnnotationSupport first searched for all methods in the type hierarchy and then applied the user-supplied predicate (or "is annotated?" predicate) afterwards. This resulted in methods in subclasses incorrectly "shadowing" package-private methods in superclasses (in a different package) even if the predicate would otherwise exclude the method in such a subclass. For example, given a superclass that declares a package-private static @BeforeAll "before()" method and a subclass (in a different package) that declares a @BeforeEach "before()" method, when JUnit Jupiter looked up @BeforeAll methods for the subclass, the @BeforeAll "before()" method in the superclass was not found because the @BeforeEach "before()" method shadowed it based solely on the method signature, ignoring the type of annotation sought. To address that, this commit modifies the internal search algorithms in ReflectionUtils so that method predicates are applied while searching the hierarchy for methods. Closes #3498 Closes #3500
This commit includes a fix with two simple test classes that demonstrate the issue. TODO: - add "formal" tests in ReflectionUtilsTests and AnnotationUtilsTests - add release note entries See junit-team#3498 Closes junit-team#3532
Prior to this commit, findFields() and streamFields() in ReflectionSupport as well as findAnnotatedFields() and findAnnotatedFieldValues() in AnnotationSupport first searched for all fields in the type hierarchy and then applied the user-supplied predicate (or "is annotated?" predicate) afterwards. This resulted in fields in subclasses incorrectly "shadowing" package-private fields in superclasses (in a different package) even if the predicate would otherwise exclude the field in such a subclass. For example, given a superclass that declares a package-private static @TempDir "tempDir" field and a subclass (in a different package) that declares a @TempDir "tempDir" field, when JUnit Jupiter looked up @TempDir fields for the subclass, the @TempDir "tempDir" field in the superclass was not found because the @TempDir "tempDir" field shadowed it based solely on the field signature, ignoring the type of annotation sought. To address that, this commit modifies the internal search algorithms in ReflectionUtils so that field predicates are applied while searching the hierarchy for fields. TODO: - add release note entries See junit-team#3498 Closes junit-team#3532
Prior to this commit, findFields() and streamFields() in ReflectionSupport as well as findAnnotatedFields() and findAnnotatedFieldValues() in AnnotationSupport first searched for all fields in the type hierarchy and then applied the user-supplied predicate (or "is annotated?" predicate) afterwards. This resulted in fields in subclasses incorrectly "shadowing" package-private fields in superclasses (in a different package) even if the predicate would otherwise exclude the field in such a subclass. For example, given a superclass that declares a package-private static @TempDir "tempDir" field and a subclass (in a different package) that declares a @TempDir "tempDir" field, when JUnit Jupiter looked up @TempDir fields for the subclass, the @TempDir "tempDir" field in the superclass was not found because the @TempDir "tempDir" field shadowed it based solely on the field signature, ignoring the type of annotation sought. To address that, this commit modifies the internal search algorithms in ReflectionUtils so that field predicates are applied while searching the hierarchy for fields. TODO: - add release note entries See junit-team#3498 Closes junit-team#3532
Prior to this commit, findFields() and streamFields() in ReflectionSupport as well as findAnnotatedFields() and findAnnotatedFieldValues() in AnnotationSupport first searched for all fields in the type hierarchy and then applied the user-supplied predicate (or "is annotated?" predicate) afterwards. This resulted in fields in subclasses incorrectly "shadowing" package-private fields in superclasses (in a different package) even if the predicate would otherwise exclude the field in such a subclass. For example, given a superclass that declares a package-private static @TempDir "tempDir" field and a subclass (in a different package) that declares a @TempDir "tempDir" field, when JUnit Jupiter looked up @TempDir fields for the subclass, the @TempDir "tempDir" field in the superclass was not found because the @TempDir "tempDir" field shadowed it based solely on the field signature, ignoring the type of annotation sought. To address that, this commit modifies the internal search algorithms in ReflectionUtils so that field predicates are applied while searching the hierarchy for fields. See junit-team#3498 Closes junit-team#3532 Closes junit-team#3533
Prior to this commit, findFields() and streamFields() in ReflectionSupport as well as findAnnotatedFields() and findAnnotatedFieldValues() in AnnotationSupport first searched for all fields in the type hierarchy and then applied the user-supplied predicate (or "is annotated?" predicate) afterwards. This resulted in fields in subclasses incorrectly "shadowing" package-private fields in superclasses (in a different package) even if the predicate would otherwise exclude the field in such a subclass. For example, given a superclass that declares a package-private static @TempDir "tempDir" field and a subclass (in a different package) that declares a @TempDir "tempDir" field, when JUnit Jupiter looked up @TempDir fields for the subclass, the @TempDir "tempDir" field in the superclass was not found because the @TempDir "tempDir" field shadowed it based solely on the field signature, ignoring the type of annotation sought. To address that, this commit modifies the internal search algorithms in ReflectionUtils so that field predicates are applied while searching the hierarchy for fields. See #3498 Closes #3532 Closes #3533
Prior to this commit, findFields() and streamFields() in ReflectionSupport as well as findAnnotatedFields() and findAnnotatedFieldValues() in AnnotationSupport first searched for all fields in the type hierarchy and then applied the user-supplied predicate (or "is annotated?" predicate) afterwards. This resulted in fields in subclasses incorrectly "shadowing" package-private fields in superclasses (in a different package) even if the predicate would otherwise exclude the field in such a subclass. For example, given a superclass that declares a package-private static @TempDir "tempDir" field and a subclass (in a different package) that declares a @TempDir "tempDir" field, when JUnit Jupiter looked up @TempDir fields for the subclass, the @TempDir "tempDir" field in the superclass was not found because the @TempDir "tempDir" field shadowed it based solely on the field signature, ignoring the type of annotation sought. To address that, this commit modifies the internal search algorithms in ReflectionUtils so that field predicates are applied while searching the hierarchy for fields. See #3498 Closes #3532 Closes #3533 (cherry picked from commit f30a8d5)
This commit consistently applies method predicates in search algorithms analogous to the application of field predicates. See junit-team#3498 See junit-team#3532 Closes junit-team#3534
This commit consistently applies method predicates in search algorithms analogous to the application of field predicates. See junit-team#3498 See junit-team#3532 Closes junit-team#3534
This has been merged into |
Closely related to: |
Steps to reproduce
Given:
and
Executing the test in
B
prints:B
andHi there
. Note the absence ofA
: the@BeforeAll
method was not called. Note that the non-staticbefore
method inB
does not override the static method (it is in a different package), nor does it conflict with it. Renaming thebefore
method inB
orA
fixes the problem.This can easily happen when you have an
abstract
base class in some utility package, and you are extending it to build tests. If you happen to name a method in this test class that conflicts with a package-private method in the base class, things do not work as expected.Workaround:
Ensure the names in the base class are sufficiently unique that they're unlikely to have similar names in a subclass.
Deliverables
The text was updated successfully, but these errors were encountered: