Search in sources :

Example 1 with MockMessageHandler

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();
    }
}
Also used : MockMessageHandler(org.springframework.integration.test.mock.MockMessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) Lifecycle(org.springframework.context.Lifecycle) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MessageProducer(org.springframework.integration.core.MessageProducer)

Aggregations

DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 Lifecycle (org.springframework.context.Lifecycle)1 MessageProducer (org.springframework.integration.core.MessageProducer)1 MockMessageHandler (org.springframework.integration.test.mock.MockMessageHandler)1 MessageChannel (org.springframework.messaging.MessageChannel)1