From ed00f62549f3ee8ab5585abfc20a2bf4cb5feac4 Mon Sep 17 00:00:00 2001 From: Yicheng Feng <1264380449@qq.com> Date: Wed, 21 Mar 2018 11:05:18 +0800 Subject: [PATCH] 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** --- .../test/context/MockIntegrationContext.java | 7 +-- .../test/mock/MockMessageHandlerTests.java | 43 ++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContext.java b/spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContext.java index 164df78665..3c9745d64d 100644 --- a/spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContext.java +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContext.java @@ -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"); * 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. * * @author Artem Bilan + * @author Yicheng Feng * * @since 5.0 + * * @see SpringIntegrationTest */ public class MockIntegrationContext implements BeanFactoryAware { @@ -139,8 +141,7 @@ public class MockIntegrationContext implements BeanFactoryAware { if (mockMessageHandler instanceof MessageProducer) { if (targetMessageHandler instanceof MessageProducer) { - MessageChannel outputChannel = TestUtils.getPropertyValue(targetMessageHandler, "outputChannel", - MessageChannel.class); + MessageChannel outputChannel = ((MessageProducer) targetMessageHandler).getOutputChannel(); ((MessageProducer) mockMessageHandler).setOutputChannel(outputChannel); } else { diff --git a/spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageHandlerTests.java b/spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageHandlerTests.java index 2af385717e..2c1aa985db 100644 --- a/spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageHandlerTests.java +++ b/spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageHandlerTests.java @@ -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"); * 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.expression.ValueExpression; import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler; +import org.springframework.integration.handler.ServiceActivatingHandler; import org.springframework.integration.support.MessageBuilder; import org.springframework.integration.test.context.MockIntegrationContext; import org.springframework.integration.test.context.SpringIntegrationTest; @@ -65,6 +66,7 @@ import org.springframework.test.context.junit4.SpringRunner; /** * @author Artem Bilan + * @author Yicheng Feng * * @since 5.0 */ @@ -85,12 +87,18 @@ public class MockMessageHandlerTests { @Autowired private MessageChannel pojoServiceChannel; + @Autowired + private MessageChannel startChannel; + @Autowired private MessageChannel rawChannel; @Autowired private QueueChannel results; + @Autowired + private ArgumentCaptor> argumentCaptorForOutputTest; + @Autowired private ArgumentCaptor> 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 message = MessageBuilder.withPayload("bar").build(); + this.startChannel.send(message); + this.startChannel.send(message); + List> list = argumentCaptorForOutputTest.getAllValues(); + assertEquals(2, list.size()); + } + @Configuration @EnableIntegration public static class Config { @@ -254,6 +278,23 @@ public class MockMessageHandlerTests { new ExpressionEvaluatingMessageHandler(new ValueExpression<>("test"))); } + @ServiceActivator(inputChannel = "startChannel", outputChannel = "nextChannel") + public String mockService(String payload) { + return payload + "test"; + } + + @Bean + public ArgumentCaptor> argumentCaptorForOutputTest() { + return MockIntegration.messageArgumentCaptor(); + } + + @Bean + @ServiceActivator(inputChannel = "nextChannel") + public MessageHandler handleNextInput() { + return mockMessageHandler(argumentCaptorForOutputTest()) + .handleNext(m -> {}); + } + } }