11/*
2- * Copyright 2018-2023 the original author or authors.
2+ * Copyright 2018-2024 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2525import static org .mockito .Mockito .mock ;
2626import static org .mockito .Mockito .times ;
2727
28- import java .lang .annotation .ElementType ;
2928import java .lang .annotation .Retention ;
3029import java .lang .annotation .RetentionPolicy ;
31- import java .lang .annotation .Target ;
3230import java .lang .reflect .Method ;
3331import java .util .Collections ;
3432
4745 * @author Tomaz Fernandes
4846 * @author Gary Russell
4947 * @author Fabio da Silva Jr.
48+ * @author Wang Zhiyang
49+ *
5050 * @since 2.7
5151 */
5252@ ExtendWith (MockitoExtension .class )
@@ -56,9 +56,7 @@ class RetryTopicConfigurationProviderTests {
5656
5757 {
5858 this .beanFactory = mock (ConfigurableListableBeanFactory .class );
59- willAnswer (invoc -> {
60- return invoc .getArgument (0 );
61- }).given (this .beanFactory ).resolveEmbeddedValue (anyString ());
59+ willAnswer (invoc -> invoc .getArgument (0 )).given (this .beanFactory ).resolveEmbeddedValue (anyString ());
6260 }
6361
6462 private final String [] topics = {"topic1" , "topic2" };
@@ -81,18 +79,12 @@ private Method getAnnotatedMethod(String methodName) {
8179 @ Mock
8280 Object bean ;
8381
84- @ Mock
85- RetryableTopic annotation ;
86-
8782 @ Mock
8883 KafkaOperations <?, ?> kafkaOperations ;
8984
9085 @ Mock
9186 RetryTopicConfiguration retryTopicConfiguration ;
9287
93- @ Mock
94- RetryTopicConfiguration retryTopicConfiguration2 ;
95-
9688 @ Test
9789 void shouldProvideFromAnnotation () {
9890
@@ -102,10 +94,13 @@ void shouldProvideFromAnnotation() {
10294 // given
10395 RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (beanFactory );
10496 RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , annotatedMethod , bean );
97+ RetryTopicConfiguration configurationFromClass = provider
98+ .findRetryConfigurationFor (topics , null , AnnotatedClass .class , bean );
10599
106100 // then
107101 then (this .beanFactory ).should (times (0 )).getBeansOfType (RetryTopicConfiguration .class );
108-
102+ assertThat (configuration ).isNotNull ();
103+ assertThat (configurationFromClass ).isNotNull ();
109104 }
110105
111106 @ Test
@@ -119,10 +114,13 @@ void shouldProvideFromBeanFactory() {
119114 // given
120115 RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (beanFactory );
121116 RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , nonAnnotatedMethod , bean );
117+ RetryTopicConfiguration configurationFromClass = provider
118+ .findRetryConfigurationFor (topics , null , NonAnnotatedClass .class , bean );
122119
123120 // then
124- then (this .beanFactory ).should (times (1 )).getBeansOfType (RetryTopicConfiguration .class );
121+ then (this .beanFactory ).should (times (2 )).getBeansOfType (RetryTopicConfiguration .class );
125122 assertThat (configuration ).isEqualTo (retryTopicConfiguration );
123+ assertThat (configurationFromClass ).isEqualTo (retryTopicConfiguration );
126124
127125 }
128126
@@ -137,10 +135,13 @@ void shouldFindNone() {
137135 // given
138136 RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (beanFactory );
139137 RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , nonAnnotatedMethod , bean );
138+ RetryTopicConfiguration configurationFromClass = provider
139+ .findRetryConfigurationFor (topics , null , NonAnnotatedClass .class , bean );
140140
141141 // then
142- then (this .beanFactory ).should (times (1 )).getBeansOfType (RetryTopicConfiguration .class );
142+ then (this .beanFactory ).should (times (2 )).getBeansOfType (RetryTopicConfiguration .class );
143143 assertThat (configuration ).isNull ();
144+ assertThat (configurationFromClass ).isNull ();
144145
145146 }
146147
@@ -153,10 +154,15 @@ void shouldProvideFromMetaAnnotation() {
153154 // given
154155 RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (beanFactory );
155156 RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , metaAnnotatedMethod , bean );
157+ RetryTopicConfiguration configurationFromClass = provider
158+ .findRetryConfigurationFor (topics , null , MetaAnnotatedClass .class , bean );
156159
157160 // then
158161 then (this .beanFactory ).should (times (0 )).getBeansOfType (RetryTopicConfiguration .class );
162+ assertThat (configuration ).isNotNull ();
159163 assertThat (configuration .getConcurrency ()).isEqualTo (3 );
164+ assertThat (configurationFromClass ).isNotNull ();
165+ assertThat (configurationFromClass .getConcurrency ()).isEqualTo (3 );
160166
161167 }
162168
@@ -166,9 +172,12 @@ void shouldNotConfigureIfBeanFactoryNull() {
166172 // given
167173 RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (null );
168174 RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , nonAnnotatedMethod , bean );
175+ RetryTopicConfiguration configurationFromClass
176+ = provider .findRetryConfigurationFor (topics , null , NonAnnotatedClass .class , bean );
169177
170178 // then
171179 assertThat (configuration ).isNull ();
180+ assertThat (configurationFromClass ).isNull ();
172181
173182 }
174183
@@ -181,7 +190,6 @@ public void nonAnnotatedMethod() {
181190 // NoOps
182191 }
183192
184- @ Target ({ElementType .METHOD })
185193 @ Retention (RetentionPolicy .RUNTIME )
186194 @ RetryableTopic
187195 @interface MetaAnnotatedRetryableTopic {
@@ -193,4 +201,19 @@ public void nonAnnotatedMethod() {
193201 public void metaAnnotatedMethod () {
194202 // NoOps
195203 }
204+
205+ @ RetryableTopic
206+ public static class AnnotatedClass {
207+ // NoOps
208+ }
209+
210+ public static class NonAnnotatedClass {
211+ // NoOps
212+ }
213+
214+ @ MetaAnnotatedRetryableTopic
215+ public static class MetaAnnotatedClass {
216+ // NoOps
217+ }
218+
196219}
0 commit comments