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**
This commit is contained in:
Yicheng Feng
2018-03-21 11:05:18 +08:00
committed by Artem Bilan
parent bcf4402b91
commit ed00f62549
2 changed files with 46 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2017 the original author or authors. * Copyright 2017-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -46,8 +46,10 @@ import org.springframework.util.ObjectUtils;
* annotation and can be autowired into test class. * annotation and can be autowired into test class.
* *
* @author Artem Bilan * @author Artem Bilan
* @author Yicheng Feng
* *
* @since 5.0 * @since 5.0
*
* @see SpringIntegrationTest * @see SpringIntegrationTest
*/ */
public class MockIntegrationContext implements BeanFactoryAware { public class MockIntegrationContext implements BeanFactoryAware {
@@ -139,8 +141,7 @@ public class MockIntegrationContext implements BeanFactoryAware {
if (mockMessageHandler instanceof MessageProducer) { if (mockMessageHandler instanceof MessageProducer) {
if (targetMessageHandler instanceof MessageProducer) { if (targetMessageHandler instanceof MessageProducer) {
MessageChannel outputChannel = TestUtils.getPropertyValue(targetMessageHandler, "outputChannel", MessageChannel outputChannel = ((MessageProducer) targetMessageHandler).getOutputChannel();
MessageChannel.class);
((MessageProducer) mockMessageHandler).setOutputChannel(outputChannel); ((MessageProducer) mockMessageHandler).setOutputChannel(outputChannel);
} }
else { else {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2017 the original author or authors. * Copyright 2017-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@ import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.endpoint.EventDrivenConsumer; import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.expression.ValueExpression; import org.springframework.integration.expression.ValueExpression;
import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler; import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.support.MessageBuilder; import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.context.MockIntegrationContext; import org.springframework.integration.test.context.MockIntegrationContext;
import org.springframework.integration.test.context.SpringIntegrationTest; import org.springframework.integration.test.context.SpringIntegrationTest;
@@ -65,6 +66,7 @@ import org.springframework.test.context.junit4.SpringRunner;
/** /**
* @author Artem Bilan * @author Artem Bilan
* @author Yicheng Feng
* *
* @since 5.0 * @since 5.0
*/ */
@@ -85,12 +87,18 @@ public class MockMessageHandlerTests {
@Autowired @Autowired
private MessageChannel pojoServiceChannel; private MessageChannel pojoServiceChannel;
@Autowired
private MessageChannel startChannel;
@Autowired @Autowired
private MessageChannel rawChannel; private MessageChannel rawChannel;
@Autowired @Autowired
private QueueChannel results; private QueueChannel results;
@Autowired
private ArgumentCaptor<Message<?>> argumentCaptorForOutputTest;
@Autowired @Autowired
private ArgumentCaptor<Message<?>> messageArgumentCaptor; private ArgumentCaptor<Message<?>> messageArgumentCaptor;
@@ -209,6 +217,22 @@ public class MockMessageHandlerTests {
} }
} }
/**
* Test whether the output channel is working after the target service activator is substituted.
*/
@Test
public void testHandlerSubstitutionWithOutputChannel() {
this.mockIntegrationContext
.substituteMessageHandlerFor("mockMessageHandlerTests.Config.mockService.serviceActivator",
mockMessageHandler()
.handleNextAndReply(m -> m));
Message<String> message = MessageBuilder.withPayload("bar").build();
this.startChannel.send(message);
this.startChannel.send(message);
List<Message<?>> list = argumentCaptorForOutputTest.getAllValues();
assertEquals(2, list.size());
}
@Configuration @Configuration
@EnableIntegration @EnableIntegration
public static class Config { public static class Config {
@@ -254,6 +278,23 @@ public class MockMessageHandlerTests {
new ExpressionEvaluatingMessageHandler(new ValueExpression<>("test"))); new ExpressionEvaluatingMessageHandler(new ValueExpression<>("test")));
} }
@ServiceActivator(inputChannel = "startChannel", outputChannel = "nextChannel")
public String mockService(String payload) {
return payload + "test";
}
@Bean
public ArgumentCaptor<Message<?>> argumentCaptorForOutputTest() {
return MockIntegration.messageArgumentCaptor();
}
@Bean
@ServiceActivator(inputChannel = "nextChannel")
public MessageHandler handleNextInput() {
return mockMessageHandler(argumentCaptorForOutputTest())
.handleNext(m -> {});
}
} }
} }