2929import reactor .test .StepVerifier ;
3030
3131import org .springframework .beans .DirectFieldAccessor ;
32- import org .springframework .beans .factory .BeanFactory ;
3332import org .springframework .integration .MessageDispatchingException ;
3433import org .springframework .integration .annotation .Gateway ;
3534import org .springframework .integration .annotation .GatewayHeader ;
4241import org .springframework .messaging .support .ChannelInterceptor ;
4342import org .springframework .messaging .support .GenericMessage ;
4443import org .springframework .messaging .support .MessageBuilder ;
45- import org .springframework .scheduling .TaskScheduler ;
44+ import org .springframework .scheduling .concurrent . SimpleAsyncTaskScheduler ;
4645import org .springframework .util .ReflectionUtils ;
4746
4847import static org .assertj .core .api .Assertions .assertThat ;
4948import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
50- import static org .mockito .ArgumentMatchers .any ;
51- import static org .mockito .ArgumentMatchers .eq ;
5249import static org .mockito .Mockito .mock ;
53- import static org .mockito .Mockito .when ;
5450
5551/**
5652 * @author Mark Fisher
@@ -67,9 +63,7 @@ public void futureWithMessageReturned() throws Exception {
6763 QueueChannel requestChannel = new QueueChannel ();
6864 startResponder (requestChannel );
6965 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
70- proxyFactory .setDefaultRequestChannel (requestChannel );
71- proxyFactory .setBeanName ("testGateway" );
72- proxyFactory .setBeanFactory (getBeanFactory ());
66+ setupProxyFactory (requestChannel , proxyFactory );
7367 proxyFactory .afterPropertiesSet ();
7468 TestEchoService service = proxyFactory .getObject ();
7569 Future <Message <?>> f = service .returnMessage ("foo" );
@@ -90,9 +84,7 @@ protected boolean doSend(Message<?> message, long timeout) {
9084
9185 };
9286 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
93- proxyFactory .setDefaultRequestChannel (channel );
94- proxyFactory .setBeanName ("testGateway" );
95- proxyFactory .setBeanFactory (getBeanFactory ());
87+ setupProxyFactory (channel , proxyFactory );
9688 proxyFactory .afterPropertiesSet ();
9789 TestEchoService service = proxyFactory .getObject ();
9890 Future <Message <?>> f = service .returnMessage ("foo" );
@@ -108,9 +100,7 @@ public void listenableFutureWithMessageReturned() throws Exception {
108100 addThreadEnricher (requestChannel );
109101 startResponder (requestChannel );
110102 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
111- proxyFactory .setDefaultRequestChannel (requestChannel );
112- proxyFactory .setBeanName ("testGateway" );
113- proxyFactory .setBeanFactory (getBeanFactory ());
103+ setupProxyFactory (requestChannel , proxyFactory );
114104 proxyFactory .afterPropertiesSet ();
115105 TestEchoService service = proxyFactory .getObject ();
116106 CompletableFuture <Message <?>> f = service .returnMessageListenable ("foo" );
@@ -134,9 +124,7 @@ public void customFutureReturned() {
134124 addThreadEnricher (requestChannel );
135125 startResponder (requestChannel );
136126 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
137- proxyFactory .setDefaultRequestChannel (requestChannel );
138- proxyFactory .setBeanName ("testGateway" );
139- proxyFactory .setBeanFactory (getBeanFactory ());
127+ setupProxyFactory (requestChannel , proxyFactory );
140128 proxyFactory .afterPropertiesSet ();
141129 TestEchoService service = proxyFactory .getObject ();
142130 CustomFuture f = service .returnCustomFuture ("foo" );
@@ -151,10 +139,7 @@ public void nonAsyncFutureReturned() {
151139 addThreadEnricher (requestChannel );
152140 startResponder (requestChannel );
153141 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
154- proxyFactory .setDefaultRequestChannel (requestChannel );
155- proxyFactory .setBeanName ("testGateway" );
156- proxyFactory .setBeanFactory (getBeanFactory ());
157-
142+ setupProxyFactory (requestChannel , proxyFactory );
158143 proxyFactory .setAsyncExecutor (null ); // Not async - user flow returns Future<?>
159144
160145 proxyFactory .afterPropertiesSet ();
@@ -183,9 +168,7 @@ public void futureWithPayloadReturned() throws Exception {
183168 QueueChannel requestChannel = new QueueChannel ();
184169 startResponder (requestChannel );
185170 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
186- proxyFactory .setDefaultRequestChannel (requestChannel );
187- proxyFactory .setBeanName ("testGateway" );
188- proxyFactory .setBeanFactory (getBeanFactory ());
171+ setupProxyFactory (requestChannel , proxyFactory );
189172 proxyFactory .afterPropertiesSet ();
190173 TestEchoService service = proxyFactory .getObject ();
191174 Future <String > f = service .returnString ("foo" );
@@ -199,9 +182,7 @@ public void futureWithWildcardReturned() throws Exception {
199182 QueueChannel requestChannel = new QueueChannel ();
200183 startResponder (requestChannel );
201184 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
202- proxyFactory .setDefaultRequestChannel (requestChannel );
203- proxyFactory .setBeanName ("testGateway" );
204- proxyFactory .setBeanFactory (getBeanFactory ());
185+ setupProxyFactory (requestChannel , proxyFactory );
205186 proxyFactory .afterPropertiesSet ();
206187 TestEchoService service = proxyFactory .getObject ();
207188 Future <?> f = service .returnSomething ("foo" );
@@ -213,9 +194,7 @@ public void futureWithWildcardReturned() throws Exception {
213194 @ Test
214195 public void futureVoid () throws Exception {
215196 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
216- proxyFactory .setDefaultRequestChannel (new NullChannel ());
217- proxyFactory .setBeanName ("testGateway" );
218- proxyFactory .setBeanFactory (getBeanFactory ());
197+ setupProxyFactory (new NullChannel (), proxyFactory );
219198 proxyFactory .afterPropertiesSet ();
220199 TestEchoService service = proxyFactory .getObject ();
221200 Future <Void > f = service .asyncSendAndForget ("test1" );
@@ -253,9 +232,7 @@ public void futureVoidReply() throws Exception {
253232 }
254233 }).start ();
255234 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
256- proxyFactory .setDefaultRequestChannel (requestChannel );
257- proxyFactory .setBeanName ("testGateway" );
258- proxyFactory .setBeanFactory (getBeanFactory ());
235+ setupProxyFactory (requestChannel , proxyFactory );
259236 proxyFactory .setAsyncExecutor (null );
260237 proxyFactory .afterPropertiesSet ();
261238 TestEchoService service = proxyFactory .getObject ();
@@ -270,9 +247,7 @@ public void monoWithMessageReturned() {
270247 QueueChannel requestChannel = new QueueChannel ();
271248 startResponder (requestChannel );
272249 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
273- proxyFactory .setDefaultRequestChannel (requestChannel );
274- proxyFactory .setBeanFactory (getBeanFactory ());
275- proxyFactory .setBeanName ("testGateway" );
250+ setupProxyFactory (requestChannel , proxyFactory );
276251 proxyFactory .afterPropertiesSet ();
277252 TestEchoService service = proxyFactory .getObject ();
278253 Mono <Message <?>> mono = service .returnMessagePromise ("foo" );
@@ -285,9 +260,7 @@ public void monoWithPayloadReturned() {
285260 QueueChannel requestChannel = new QueueChannel ();
286261 startResponder (requestChannel );
287262 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
288- proxyFactory .setDefaultRequestChannel (requestChannel );
289- proxyFactory .setBeanFactory (getBeanFactory ());
290- proxyFactory .setBeanName ("testGateway" );
263+ setupProxyFactory (requestChannel , proxyFactory );
291264 proxyFactory .afterPropertiesSet ();
292265 TestEchoService service = proxyFactory .getObject ();
293266 Mono <String > mono = service .returnStringPromise ("foo" );
@@ -300,9 +273,7 @@ public void monoWithWildcardReturned() {
300273 QueueChannel requestChannel = new QueueChannel ();
301274 startResponder (requestChannel );
302275 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
303- proxyFactory .setDefaultRequestChannel (requestChannel );
304- proxyFactory .setBeanFactory (getBeanFactory ());
305- proxyFactory .setBeanName ("testGateway" );
276+ setupProxyFactory (requestChannel , proxyFactory );
306277 proxyFactory .afterPropertiesSet ();
307278 TestEchoService service = proxyFactory .getObject ();
308279 Mono <?> mono = service .returnSomethingPromise ("foo" );
@@ -316,9 +287,7 @@ public void monoWithConsumer() {
316287 QueueChannel requestChannel = new QueueChannel ();
317288 startResponder (requestChannel );
318289 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
319- proxyFactory .setDefaultRequestChannel (requestChannel );
320- proxyFactory .setBeanFactory (getBeanFactory ());
321- proxyFactory .setBeanName ("testGateway" );
290+ setupProxyFactory (requestChannel , proxyFactory );
322291 proxyFactory .afterPropertiesSet ();
323292 TestEchoService service = proxyFactory .getObject ();
324293 Mono <String > mono = service .returnStringPromise ("foo" );
@@ -331,9 +300,7 @@ public void monoWithConsumer() {
331300 @ Test
332301 public void monoVoid () throws InterruptedException {
333302 GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
334- proxyFactory .setDefaultRequestChannel (new NullChannel ());
335- proxyFactory .setBeanName ("testGateway" );
336- proxyFactory .setBeanFactory (getBeanFactory ());
303+ setupProxyFactory (new NullChannel (), proxyFactory );
337304 proxyFactory .afterPropertiesSet ();
338305 TestEchoService service = proxyFactory .getObject ();
339306 Mono <Void > mono = service .monoVoid ("test1" );
@@ -376,15 +343,6 @@ private static void startResponder(final PollableChannel requestChannel) {
376343 }).start ();
377344 }
378345
379- private BeanFactory getBeanFactory () {
380- BeanFactory beanFactory = mock (BeanFactory .class );
381- TaskScheduler taskScheduler = mock (TaskScheduler .class );
382- when (beanFactory .getBean (eq ("taskScheduler" ), any (Class .class )))
383- .thenReturn (taskScheduler );
384- when (beanFactory .containsBean ("taskScheduler" )).thenReturn (true );
385- return beanFactory ;
386- }
387-
388346 private interface TestEchoService {
389347
390348 Future <String > returnString (String s );
@@ -415,6 +373,13 @@ private interface TestEchoService {
415373
416374 }
417375
376+ private static void setupProxyFactory (MessageChannel messageChannel , GatewayProxyFactoryBean proxyFactory ) {
377+ proxyFactory .setDefaultRequestChannel (messageChannel );
378+ proxyFactory .setBeanName ("testGateway" );
379+ proxyFactory .setTaskScheduler (new SimpleAsyncTaskScheduler ());
380+ proxyFactory .setBeanFactory (mock ());
381+ }
382+
418383 private record CustomFuture (String result , Thread thread ) implements Future <String > {
419384
420385 @ Override
0 commit comments