2323import java .time .LocalDate ;
2424import java .util .ArrayList ;
2525import java .util .List ;
26+ import java .util .function .Supplier ;
2627import java .util .stream .Stream ;
2728
2829import org .jspecify .annotations .Nullable ;
2930import org .junit .jupiter .api .DisplayName ;
31+ import org .junit .jupiter .api .Named ;
3032import org .junit .jupiter .api .Test ;
3133import org .junit .jupiter .api .extension .ExtensionContext ;
3234import org .junit .jupiter .api .extension .ParameterContext ;
35+ import org .junit .jupiter .params .ParameterizedTest ;
3336import org .junit .jupiter .params .converter .AnnotationBasedArgumentConverter ;
3437import org .junit .jupiter .params .converter .JavaTimeConversionPattern ;
3538import org .junit .jupiter .params .provider .AnnotationBasedArgumentsProvider ;
3639import org .junit .jupiter .params .provider .Arguments ;
3740import org .junit .jupiter .params .provider .CsvSource ;
41+ import org .junit .jupiter .params .provider .FieldSource ;
3842import 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