Skip to content

Commit 4bbe57d

Browse files
authored
Fix support pre-5.13 AnnotationBasedArgumentsProvider implementations (#4611)
The `provideArguments(ExtensionContext, Annotation)` was deprecated in 5.13 and no longer taken into account when determining the consumed annotation. Now, it is checked for again if the new method overload couldn't be found. Fixes #4610.
1 parent d52ddac commit 4bbe57d

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.13.1.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ on GitHub.
4242
in their enclosing class. This undesired change in behavior has now been reverted so
4343
that tests in `@Nested` test classes are always executed _after_ tests in enclosing test
4444
classes again.
45+
* Fix support for `AnnotationBasedArgumentsProvider` implementations that override the
46+
deprecated `provideArguments(ExtensionContext, Annotation)` method.
4547

4648
[[release-notes-5.13.1-junit-jupiter-deprecations-and-breaking-changes]]
4749
==== Deprecations and Breaking Changes

junit-jupiter-params/src/main/java/org/junit/jupiter/params/support/AnnotationConsumerInitializer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public final class AnnotationConsumerInitializer {
4141
private static final List<AnnotationConsumingMethodSignature> annotationConsumingMethodSignatures = asList( //
4242
new AnnotationConsumingMethodSignature("accept", 1, 0), //
4343
new AnnotationConsumingMethodSignature("provideArguments", 3, 2), //
44+
new AnnotationConsumingMethodSignature("provideArguments", 2, 1), //
4445
new AnnotationConsumingMethodSignature("convert", 3, 2));
4546

4647
private AnnotationConsumerInitializer() {

jupiter-tests/src/test/java/org/junit/jupiter/params/support/AnnotationConsumerInitializerTests.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,22 @@
2323
import java.time.LocalDate;
2424
import java.util.ArrayList;
2525
import java.util.List;
26+
import java.util.function.Supplier;
2627
import java.util.stream.Stream;
2728

2829
import org.jspecify.annotations.Nullable;
2930
import org.junit.jupiter.api.DisplayName;
31+
import org.junit.jupiter.api.Named;
3032
import org.junit.jupiter.api.Test;
3133
import org.junit.jupiter.api.extension.ExtensionContext;
3234
import org.junit.jupiter.api.extension.ParameterContext;
35+
import org.junit.jupiter.params.ParameterizedTest;
3336
import org.junit.jupiter.params.converter.AnnotationBasedArgumentConverter;
3437
import org.junit.jupiter.params.converter.JavaTimeConversionPattern;
3538
import org.junit.jupiter.params.provider.AnnotationBasedArgumentsProvider;
3639
import org.junit.jupiter.params.provider.Arguments;
3740
import org.junit.jupiter.params.provider.CsvSource;
41+
import org.junit.jupiter.params.provider.FieldSource;
3842
import org.junit.platform.commons.JUnitException;
3943

4044
@DisplayName("AnnotationConsumerInitializer")
@@ -52,10 +56,11 @@ void shouldInitializeAnnotationConsumer() throws NoSuchMethodException {
5256
source -> assertThat(source.value()).containsExactly("a", "b"));
5357
}
5458

55-
@Test
59+
@ParameterizedTest
60+
@FieldSource("argumentsProviders")
5661
@DisplayName("should initialize annotation-based ArgumentsProvider")
57-
void shouldInitializeAnnotationBasedArgumentsProvider() throws NoSuchMethodException {
58-
var instance = new SomeAnnotationBasedArgumentsProvider();
62+
void shouldInitializeAnnotationBasedArgumentsProvider(AbstractAnnotationBasedArgumentsProvider instance)
63+
throws NoSuchMethodException {
5964
var method = SubjectClass.class.getDeclaredMethod("foo");
6065
var initialisedAnnotationConsumer = initialize(method, instance);
6166

@@ -102,20 +107,32 @@ void shouldThrowExceptionWhenParameterIsNotAnnotated() throws NoSuchMethodExcept
102107
assertThatThrownBy(() -> initialize(parameter, instance)).isInstanceOf(JUnitException.class);
103108
}
104109

105-
@Test
106-
void shouldInitializeForEachAnnotations() throws NoSuchMethodException {
107-
var instance = spy(new SomeAnnotationBasedArgumentsProvider());
110+
@ParameterizedTest
111+
@FieldSource("argumentsProviders")
112+
void shouldInitializeForEachAnnotations(AbstractAnnotationBasedArgumentsProvider provider)
113+
throws NoSuchMethodException {
114+
var instance = spy(provider);
108115
var method = SubjectClass.class.getDeclaredMethod("repeatableAnnotation", String.class);
109116

110117
initialize(method, instance);
111118

112119
verify(instance, times(2)).accept(any(CsvSource.class));
113120
}
114121

115-
private static class SomeAnnotationBasedArgumentsProvider extends AnnotationBasedArgumentsProvider<CsvSource> {
122+
static Supplier<List<Named<? extends AbstractAnnotationBasedArgumentsProvider>>> argumentsProviders = () -> List.of( //
123+
Named.of("current", new SomeAnnotationBasedArgumentsProvider()), //
124+
Named.of("deprecated", new DeprecatedAnnotationBasedArgumentsProvider()) //
125+
);
126+
127+
private static abstract class AbstractAnnotationBasedArgumentsProvider
128+
extends AnnotationBasedArgumentsProvider<CsvSource> {
116129

117130
List<CsvSource> annotations = new ArrayList<>();
118131

132+
}
133+
134+
private static class SomeAnnotationBasedArgumentsProvider extends AbstractAnnotationBasedArgumentsProvider {
135+
119136
@Override
120137
protected Stream<? extends Arguments> provideArguments(ParameterDeclarations parameters,
121138
ExtensionContext context, CsvSource annotation) {
@@ -124,6 +141,16 @@ protected Stream<? extends Arguments> provideArguments(ParameterDeclarations par
124141
}
125142
}
126143

144+
private static class DeprecatedAnnotationBasedArgumentsProvider extends AbstractAnnotationBasedArgumentsProvider {
145+
146+
@Override
147+
@SuppressWarnings("deprecation")
148+
protected Stream<? extends Arguments> provideArguments(ExtensionContext context, CsvSource annotation) {
149+
annotations.add(annotation);
150+
return Stream.empty();
151+
}
152+
}
153+
127154
private static class SomeAnnotationBasedArgumentConverter
128155
extends AnnotationBasedArgumentConverter<JavaTimeConversionPattern> {
129156

0 commit comments

Comments
 (0)