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