Skip to content

Commit ed00f62

Browse files
fifmanartembilan
authored andcommitted
INT-4432: Mock Handler: fix output channel
JIRA: https://jira.spring.io/browse/INT-4432 The `MockIntegrationContext.substituteMessageHandlerFor()` method cannot get the correct output channel of target message handler since the `outputChannel` property is not initialized. * Use `getOutputChannel()` method instead of directly accessing the `outputChannel` property. The `getOutputChannel()` method guarantees the correct value is retrieved. * Polishing Copyrights and author name * Simplify `MockMessageHandlerTests.testHandlerSubstitutionWithOutputChannel()` and its configuration **Cherry-pick to 5.0.x**
1 parent bcf4402 commit ed00f62

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -46,8 +46,10 @@
4646
* annotation and can be autowired into test class.
4747
*
4848
* @author Artem Bilan
49+
* @author Yicheng Feng
4950
*
5051
* @since 5.0
52+
*
5153
* @see SpringIntegrationTest
5254
*/
5355
public class MockIntegrationContext implements BeanFactoryAware {
@@ -139,8 +141,7 @@ public void substituteMessageHandlerFor(String consumerEndpointId, MessageHandle
139141

140142
if (mockMessageHandler instanceof MessageProducer) {
141143
if (targetMessageHandler instanceof MessageProducer) {
142-
MessageChannel outputChannel = TestUtils.getPropertyValue(targetMessageHandler, "outputChannel",
143-
MessageChannel.class);
144+
MessageChannel outputChannel = ((MessageProducer) targetMessageHandler).getOutputChannel();
144145
((MessageProducer) mockMessageHandler).setOutputChannel(outputChannel);
145146
}
146147
else {

spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageHandlerTests.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -49,6 +49,7 @@
4949
import org.springframework.integration.endpoint.EventDrivenConsumer;
5050
import org.springframework.integration.expression.ValueExpression;
5151
import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler;
52+
import org.springframework.integration.handler.ServiceActivatingHandler;
5253
import org.springframework.integration.support.MessageBuilder;
5354
import org.springframework.integration.test.context.MockIntegrationContext;
5455
import org.springframework.integration.test.context.SpringIntegrationTest;
@@ -65,6 +66,7 @@
6566

6667
/**
6768
* @author Artem Bilan
69+
* @author Yicheng Feng
6870
*
6971
* @since 5.0
7072
*/
@@ -85,12 +87,18 @@ public class MockMessageHandlerTests {
8587
@Autowired
8688
private MessageChannel pojoServiceChannel;
8789

90+
@Autowired
91+
private MessageChannel startChannel;
92+
8893
@Autowired
8994
private MessageChannel rawChannel;
9095

9196
@Autowired
9297
private QueueChannel results;
9398

99+
@Autowired
100+
private ArgumentCaptor<Message<?>> argumentCaptorForOutputTest;
101+
94102
@Autowired
95103
private ArgumentCaptor<Message<?>> messageArgumentCaptor;
96104

@@ -209,6 +217,22 @@ public void testMockRawHandler() {
209217
}
210218
}
211219

220+
/**
221+
* Test whether the output channel is working after the target service activator is substituted.
222+
*/
223+
@Test
224+
public void testHandlerSubstitutionWithOutputChannel() {
225+
this.mockIntegrationContext
226+
.substituteMessageHandlerFor("mockMessageHandlerTests.Config.mockService.serviceActivator",
227+
mockMessageHandler()
228+
.handleNextAndReply(m -> m));
229+
Message<String> message = MessageBuilder.withPayload("bar").build();
230+
this.startChannel.send(message);
231+
this.startChannel.send(message);
232+
List<Message<?>> list = argumentCaptorForOutputTest.getAllValues();
233+
assertEquals(2, list.size());
234+
}
235+
212236
@Configuration
213237
@EnableIntegration
214238
public static class Config {
@@ -254,6 +278,23 @@ public EventDrivenConsumer rawHandlerConsumer() {
254278
new ExpressionEvaluatingMessageHandler(new ValueExpression<>("test")));
255279
}
256280

281+
@ServiceActivator(inputChannel = "startChannel", outputChannel = "nextChannel")
282+
public String mockService(String payload) {
283+
return payload + "test";
284+
}
285+
286+
@Bean
287+
public ArgumentCaptor<Message<?>> argumentCaptorForOutputTest() {
288+
return MockIntegration.messageArgumentCaptor();
289+
}
290+
291+
@Bean
292+
@ServiceActivator(inputChannel = "nextChannel")
293+
public MessageHandler handleNextInput() {
294+
return mockMessageHandler(argumentCaptorForOutputTest())
295+
.handleNext(m -> {});
296+
}
297+
257298
}
258299

259300
}

0 commit comments

Comments
 (0)