23
23
import java .time .LocalDate ;
24
24
import java .util .ArrayList ;
25
25
import java .util .List ;
26
+ import java .util .function .Supplier ;
26
27
import java .util .stream .Stream ;
27
28
28
29
import org .jspecify .annotations .Nullable ;
29
30
import org .junit .jupiter .api .DisplayName ;
31
+ import org .junit .jupiter .api .Named ;
30
32
import org .junit .jupiter .api .Test ;
31
33
import org .junit .jupiter .api .extension .ExtensionContext ;
32
34
import org .junit .jupiter .api .extension .ParameterContext ;
35
+ import org .junit .jupiter .params .ParameterizedTest ;
33
36
import org .junit .jupiter .params .converter .AnnotationBasedArgumentConverter ;
34
37
import org .junit .jupiter .params .converter .JavaTimeConversionPattern ;
35
38
import org .junit .jupiter .params .provider .AnnotationBasedArgumentsProvider ;
36
39
import org .junit .jupiter .params .provider .Arguments ;
37
40
import org .junit .jupiter .params .provider .CsvSource ;
41
+ import org .junit .jupiter .params .provider .FieldSource ;
38
42
import org .junit .platform .commons .JUnitException ;
39
43
40
44
@ DisplayName ("AnnotationConsumerInitializer" )
@@ -52,10 +56,11 @@ void shouldInitializeAnnotationConsumer() throws NoSuchMethodException {
52
56
source -> assertThat (source .value ()).containsExactly ("a" , "b" ));
53
57
}
54
58
55
- @ Test
59
+ @ ParameterizedTest
60
+ @ FieldSource ("argumentsProviders" )
56
61
@ DisplayName ("should initialize annotation-based ArgumentsProvider" )
57
- void shouldInitializeAnnotationBasedArgumentsProvider () throws NoSuchMethodException {
58
- var instance = new SomeAnnotationBasedArgumentsProvider ();
62
+ void shouldInitializeAnnotationBasedArgumentsProvider (AbstractAnnotationBasedArgumentsProvider instance )
63
+ throws NoSuchMethodException {
59
64
var method = SubjectClass .class .getDeclaredMethod ("foo" );
60
65
var initialisedAnnotationConsumer = initialize (method , instance );
61
66
@@ -102,20 +107,32 @@ void shouldThrowExceptionWhenParameterIsNotAnnotated() throws NoSuchMethodExcept
102
107
assertThatThrownBy (() -> initialize (parameter , instance )).isInstanceOf (JUnitException .class );
103
108
}
104
109
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 );
108
115
var method = SubjectClass .class .getDeclaredMethod ("repeatableAnnotation" , String .class );
109
116
110
117
initialize (method , instance );
111
118
112
119
verify (instance , times (2 )).accept (any (CsvSource .class ));
113
120
}
114
121
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 > {
116
129
117
130
List <CsvSource > annotations = new ArrayList <>();
118
131
132
+ }
133
+
134
+ private static class SomeAnnotationBasedArgumentsProvider extends AbstractAnnotationBasedArgumentsProvider {
135
+
119
136
@ Override
120
137
protected Stream <? extends Arguments > provideArguments (ParameterDeclarations parameters ,
121
138
ExtensionContext context , CsvSource annotation ) {
@@ -124,6 +141,16 @@ protected Stream<? extends Arguments> provideArguments(ParameterDeclarations par
124
141
}
125
142
}
126
143
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
+
127
154
private static class SomeAnnotationBasedArgumentConverter
128
155
extends AnnotationBasedArgumentConverter <JavaTimeConversionPattern > {
129
156
0 commit comments