Skip to content

Commit af5e3bd

Browse files
committed
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. (cherry picked from commit 4bbe57d)
1 parent 4170597 commit af5e3bd

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,17 +23,21 @@
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.junit.jupiter.api.DisplayName;
30+
import org.junit.jupiter.api.Named;
2931
import org.junit.jupiter.api.Test;
3032
import org.junit.jupiter.api.extension.ExtensionContext;
3133
import org.junit.jupiter.api.extension.ParameterContext;
34+
import org.junit.jupiter.params.ParameterizedTest;
3235
import org.junit.jupiter.params.converter.AnnotationBasedArgumentConverter;
3336
import org.junit.jupiter.params.converter.JavaTimeConversionPattern;
3437
import org.junit.jupiter.params.provider.AnnotationBasedArgumentsProvider;
3538
import org.junit.jupiter.params.provider.Arguments;
3639
import org.junit.jupiter.params.provider.CsvSource;
40+
import org.junit.jupiter.params.provider.FieldSource;
3741
import org.junit.platform.commons.JUnitException;
3842

3943
@DisplayName("AnnotationConsumerInitializer")
@@ -51,10 +55,11 @@ void shouldInitializeAnnotationConsumer() throws NoSuchMethodException {
5155
source -> assertThat(source.value()).containsExactly("a", "b"));
5256
}
5357

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

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

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

109116
initialize(method, instance);
110117

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

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

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

131+
}
132+
133+
private static class SomeAnnotationBasedArgumentsProvider extends AbstractAnnotationBasedArgumentsProvider {
134+
118135
@Override
119136
protected Stream<? extends Arguments> provideArguments(ParameterDeclarations parameters,
120137
ExtensionContext context, CsvSource annotation) {
@@ -123,6 +140,16 @@ protected Stream<? extends Arguments> provideArguments(ParameterDeclarations par
123140
}
124141
}
125142

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

0 commit comments

Comments
 (0)