Improvements for MockIntegration
This commit is contained in:
committed by
Gary Russell
parent
3daeaab1b4
commit
57feb5262f
@@ -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();
|
||||
|
||||
@@ -128,7 +128,12 @@ public final class MockIntegration {
|
||||
* @return the MockMessageHandler instance ready for interaction
|
||||
*/
|
||||
public static MockMessageHandler mockMessageHandler(ArgumentCaptor<Message<?>> messageArgumentCaptor) {
|
||||
return new MockMessageHandler(messageArgumentCaptor);
|
||||
return Mockito.spy(new MockMessageHandler(messageArgumentCaptor));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static ArgumentCaptor<Message<?>> messageArgumentCaptor() {
|
||||
return ArgumentCaptor.forClass(Message.class);
|
||||
}
|
||||
|
||||
private MockIntegration() {
|
||||
|
||||
@@ -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<Message<?>> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
|
||||
ArgumentCaptor<Message<?>> 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<Message<?>> messageArgumentCaptor() {
|
||||
return ArgumentCaptor.forClass(Message.class);
|
||||
return MockIntegration.messageArgumentCaptor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -222,7 +222,7 @@ The `MockIntegrationContext` is aimed to be used in the target test-cases for mo
|
||||
public void testMockMessageSource() {
|
||||
MessageSource<String> 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<Message<?>>` 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<String> message = new GenericMessage<>("foo");
|
||||
this.myChannel.send(message);
|
||||
Message<?> received = this.results.receive(10000);
|
||||
|
||||
Reference in New Issue
Block a user