Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Spring Integration

I am new to Spring Integration. I have ActiveMQ with say a 'responseQ'. So when a message arrives on 'responseQ' -> painResponseChannel -> transformer -> processResponseChannel -> beanProcessing. I have following setup:

    <jms:message-driven-channel-adapter  extract-payload="true"
                                     channel="painResponseChannel"
                                     connection-factory="connectionFactory"
                                     destination-name="responseQ"/>

    <integration:channel id="painResponseChannel" />

    <integration-xml:unmarshalling-transformer
        id="defaultUnmarshaller"
        input-channel="painResponseChannel"
        output-channel="processResponseChannel"
        unmarshaller="marshaller"/>

    <integration:channel id="processResponseChannel" />

    <integration:service-activator
        input-channel="processResponseChannel"
        ref="processResponseActivator"/>

    <bean id="processResponseActivator" class="com.messaging.processor.PainResponseProcessor"/>


    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
      <property name="classesToBeBound">
        <list>
            <value>com.domain.pain.Document</value>
        </list>
      </property>
    </bean>

So my question is HOW CAN I TEST THIS END TO END? How can I assert the output of the transformer or assert whats on the channel? I have tried but failed... Hope someone can help.

Thanks in advance. GM

I was testing like this: In my test-context created a outbound-channel-adapter which initiates putting a message on the activeMQ using the testJmsQueue channel. And also created a BRIDGE for the processResponseChannel -> testChannel. I was expecting the receive() method to give me something back. But I think the issue is that it too fast and by the time it gets to the receive() method the pipeline has ended.

The test-context looks like this:

<integration:bridge input-channel="processResponseChannel" output-channel="testChannel"/>

<jms:outbound-channel-adapter id="jmsOut" destination-name="responseQ" channel="testJmsQueue"/>

<integration:channel id="testJmsQueue"/>

<integration:channel id="testChannel">
    <integration:queue/>
</integration:channel>

and then in the unit test I have this:

@ContextConfiguration(locations = "classpath*:PainResponseTest-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class PainResponseTest {

private String painResponseXML;

@Autowired
MessageChannel testJmsQueue;
@Autowired
QueueChannel testChannel;

@Before
public void setup() throws Exception {

    ClassPathResource cpr = new ClassPathResource("painResponse.xml");
    InputStream is = cpr.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    painResponseXML = writer.toString();
}

@Test
@SuppressWarnings("unchecked")
public void shouldDoSomething() throws InterruptedException {

    testJmsQueue.send(MessageBuilder.withPayload(painResponseXML).build());

    Message<String> reply = (Message<String>) testChannel.receive(0);
    Assert.assertNotNull("reply should not be null", reply);
    String out = reply.getPayload();
    System.out.println(out);
}
}

==================== TEST OUTPUT =====================
java.lang.AssertionError: reply should not be null

Getting reply as null.

like image 446
user2279337 Avatar asked Apr 25 '13 16:04

user2279337


People also ask

Does Spring support integration testing?

The Spring Framework provides first-class support for integration testing in the spring-test module. The name of the actual JAR file might include the release version and might also be in the long org. springframework.


1 Answers

For end-to-end testing, you can do the following;

  • use activemq in an embedded configuration to send the JMS message
  • inject a channelinterceptor on the processResponseChannel
  • enable DEBUG level - Spring Integration gives very good and helpful logs tracing messages in and out of channels and service activators
like image 182
incomplete-co.de Avatar answered Oct 12 '22 17:10

incomplete-co.de