use of org.springframework.integration.test.mock.MockMessageHandler in project spring-integration by spring-projects.
the class MockIntegrationContext method substituteMessageHandlerFor.
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();
}
DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
Object targetMessageHandler = directFieldAccessor.getPropertyValue("handler");
this.beans.put(consumerEndpointId, targetMessageHandler);
if (mockMessageHandler instanceof MessageProducer) {
if (targetMessageHandler instanceof MessageProducer) {
MessageChannel outputChannel = ((MessageProducer) targetMessageHandler).getOutputChannel();
((MessageProducer) mockMessageHandler).setOutputChannel(outputChannel);
} else {
if (mockMessageHandler instanceof MockMessageHandler) {
if (TestUtils.getPropertyValue(mockMessageHandler, "hasReplies", Boolean.class)) {
throw new IllegalStateException("The [" + mockMessageHandler + "] " + "with replies can't replace simple MessageHandler [" + targetMessageHandler + "]");
}
} else {
throw new IllegalStateException("The MessageProducer handler [" + mockMessageHandler + "] " + "can't replace simple MessageHandler [" + targetMessageHandler + "]");
}
}
}
directFieldAccessor.setPropertyValue("handler", mockMessageHandler);
if (autoStartup && endpoint instanceof Lifecycle) {
((Lifecycle) endpoint).start();
}
}
Aggregations