From 57feb5262fa988d27fb550b396b7b53e76ccbe84 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 23 May 2017 17:22:58 -0400 Subject: [PATCH] Improvements for `MockIntegration` --- .../test/context/MockIntegrationContext.java | 21 +++++++++++-------- .../test/mock/MockIntegration.java | 7 ++++++- .../test/mock/MockMessageHandlerTests.java | 15 +++++++------ .../test/mock/MockMessageSourceTests.java | 4 ++-- src/reference/asciidoc/testing.adoc | 9 ++++---- 5 files changed, 32 insertions(+), 24 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 8c17f091d5..164df78665 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 @@ -103,8 +103,8 @@ public class MockIntegrationContext implements BeanFactoryAware { * @param mockMessageSource the {@link MessageSource} to replace in the endpoint bean * @see org.springframework.integration.test.mock.MockIntegration#mockMessageSource */ - public void instead(String pollingAdapterId, MessageSource mockMessageSource) { - instead(pollingAdapterId, mockMessageSource, true); + public void substituteMessageSourceFor(String pollingAdapterId, MessageSource mockMessageSource) { + substituteMessageSourceFor(pollingAdapterId, mockMessageSource, true); } /** @@ -117,15 +117,18 @@ public class MockIntegrationContext implements BeanFactoryAware { * @param autoStartup start or not the endpoint after replacing its {@link MessageSource} * @see org.springframework.integration.test.mock.MockIntegration#mockMessageSource */ - public void instead(String pollingAdapterId, MessageSource mockMessageSource, boolean autoStartup) { - instead(pollingAdapterId, mockMessageSource, SourcePollingChannelAdapter.class, "source", autoStartup); + public void substituteMessageSourceFor(String pollingAdapterId, MessageSource mockMessageSource, + boolean autoStartup) { + substituteMessageSourceFor(pollingAdapterId, mockMessageSource, SourcePollingChannelAdapter.class, "source", + autoStartup); } - public void instead(String consumerEndpointId, MessageHandler mockMessageHandler) { - instead(consumerEndpointId, mockMessageHandler, true); + public void substituteMessageHandlerFor(String consumerEndpointId, MessageHandler mockMessageHandler) { + substituteMessageHandlerFor(consumerEndpointId, mockMessageHandler, true); } - public void instead(String consumerEndpointId, MessageHandler mockMessageHandler, boolean autoStartup) { + public void substituteMessageHandlerFor(String consumerEndpointId, MessageHandler mockMessageHandler, + boolean autoStartup) { Object endpoint = this.beanFactory.getBean(consumerEndpointId, IntegrationConsumer.class); if (autoStartup && endpoint instanceof Lifecycle) { ((Lifecycle) endpoint).stop(); @@ -161,8 +164,8 @@ public class MockIntegrationContext implements BeanFactoryAware { } } - private void instead(String endpointId, Object messagingComponent, Class endpointClass, String property, - boolean autoStartup) { + private void substituteMessageSourceFor(String endpointId, Object messagingComponent, Class endpointClass, + String property, boolean autoStartup) { Object endpoint = this.beanFactory.getBean(endpointId, endpointClass); if (autoStartup && endpoint instanceof Lifecycle) { ((Lifecycle) endpoint).stop(); diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/mock/MockIntegration.java b/spring-integration-test/src/main/java/org/springframework/integration/test/mock/MockIntegration.java index 64bdd23f0c..309b632659 100644 --- a/spring-integration-test/src/main/java/org/springframework/integration/test/mock/MockIntegration.java +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/mock/MockIntegration.java @@ -128,7 +128,12 @@ public final class MockIntegration { * @return the MockMessageHandler instance ready for interaction */ public static MockMessageHandler mockMessageHandler(ArgumentCaptor> messageArgumentCaptor) { - return new MockMessageHandler(messageArgumentCaptor); + return Mockito.spy(new MockMessageHandler(messageArgumentCaptor)); + } + + @SuppressWarnings("unchecked") + public static ArgumentCaptor> messageArgumentCaptor() { + return ArgumentCaptor.forClass(Message.class); } private MockIntegration() { 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 96f5de08a7..2af385717e 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 @@ -152,8 +152,9 @@ public class MockMessageHandlerTests { mockMessageHandler() .handleNextAndReply(m -> m.getPayload().toString().toUpperCase()); - this.mockIntegrationContext.instead("mockMessageHandlerTests.Config.myService.serviceActivator", - mockMessageHandler); + this.mockIntegrationContext + .substituteMessageHandlerFor("mockMessageHandlerTests.Config.myService.serviceActivator", + mockMessageHandler); this.pojoServiceChannel.send(new GenericMessage<>("foo")); receive = this.results.receive(10000); @@ -171,15 +172,14 @@ public class MockMessageHandlerTests { } @Test - @SuppressWarnings("unchecked") public void testMockRawHandler() { - ArgumentCaptor> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); + ArgumentCaptor> messageArgumentCaptor = MockIntegration.messageArgumentCaptor(); MessageHandler mockMessageHandler = spy(mockMessageHandler(messageArgumentCaptor)) .handleNext(m -> { }); String endpointId = "rawHandlerConsumer"; - this.mockIntegrationContext.instead(endpointId, mockMessageHandler); + this.mockIntegrationContext.substituteMessageHandlerFor(endpointId, mockMessageHandler); Object endpoint = this.context.getBean(endpointId); assertSame(mockMessageHandler, TestUtils.getPropertyValue(endpoint, "handler", MessageHandler.class)); @@ -200,7 +200,7 @@ public class MockMessageHandlerTests { .handleNextAndReply(m -> m); try { - this.mockIntegrationContext.instead(endpointId, mockMessageHandler); + this.mockIntegrationContext.substituteMessageHandlerFor(endpointId, mockMessageHandler); fail("IllegalStateException expected"); } catch (Exception e) { @@ -219,9 +219,8 @@ public class MockMessageHandlerTests { } @Bean - @SuppressWarnings("unchecked") public ArgumentCaptor> messageArgumentCaptor() { - return ArgumentCaptor.forClass(Message.class); + return MockIntegration.messageArgumentCaptor(); } @Bean diff --git a/spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageSourceTests.java b/spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageSourceTests.java index 98b5032a2e..9cbc772f64 100644 --- a/spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageSourceTests.java +++ b/spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageSourceTests.java @@ -85,7 +85,7 @@ public class MockMessageSourceTests { @Test public void testMockMessageSource() { - this.mockIntegrationContext.instead("mySourceEndpoint", + this.mockIntegrationContext.substituteMessageSourceFor("mySourceEndpoint", MockIntegration.mockMessageSource("foo", "bar", "baz")); Message receive = this.results.receive(10_000); @@ -179,7 +179,7 @@ public class MockMessageSourceTests { @Test public void testWrongBeanForInstead() { try { - this.mockIntegrationContext.instead("errorChannel", () -> null); + this.mockIntegrationContext.substituteMessageSourceFor("errorChannel", () -> null); fail("BeanNotOfRequiredTypeException expected"); } catch (Exception e) { diff --git a/src/reference/asciidoc/testing.adoc b/src/reference/asciidoc/testing.adoc index a06b67a56f..4487408c21 100644 --- a/src/reference/asciidoc/testing.adoc +++ b/src/reference/asciidoc/testing.adoc @@ -222,7 +222,7 @@ The `MockIntegrationContext` is aimed to be used in the target test-cases for mo public void testMockMessageSource() { MessageSource messageSource = () -> new GenericMessage<>("foo"); - this.mockIntegrationContext.instead("mySourceEndpoint", messageSource); + this.mockIntegrationContext.substituteMessageSourceFor("mySourceEndpoint", messageSource); Message receive = this.results.receive(10_000); assertNotNull(receive); @@ -280,7 +280,7 @@ For this purpose, the aforementioned `MockIntegrationContext` should be used fro [source,java] ---- -this.mockIntegrationContext.instead("mySourceEndpoint", +this.mockIntegrationContext.substituteMessageSourceFor("mySourceEndpoint", MockIntegration.mockMessageSource("foo", "bar", "baz")); Message receive = this.results.receive(10_000); assertNotNull(receive); @@ -298,7 +298,7 @@ In addition, a Mockito `ArgumentCaptor>` can be supplied to the `Mock Each request message for the `MockMessageHandler` is captured by that `ArgumentCaptor`. During the test, its `getValue()/getAllValues()` can be used to verify and assert those request messages. -The `MockIntegrationContext` provides an `instead()` API for replacing the actual configured `MessageHandler` with a `MockMessageHandler`, in the particular endpoint in the application context under test. +The `MockIntegrationContext` provides an `substituteMessageHandlerFor()` API for replacing the actual configured `MessageHandler` with a `MockMessageHandler`, in the particular endpoint in the application context under test. A typical usage might be: @@ -310,7 +310,8 @@ MessageHandler mockMessageHandler = mockMessageHandler(messageArgumentCaptor) .handleNextAndReply(m -> m.getPayload().toString().toUpperCase()); -this.mockIntegrationContext.instead("myService.serviceActivator", mockMessageHandler); +this.mockIntegrationContext.substituteMessageHandlerFor("myService.serviceActivator", + mockMessageHandler); GenericMessage message = new GenericMessage<>("foo"); this.myChannel.send(message); Message received = this.results.receive(10000);