Citrus testcase example: CSV File Inbound -> Xml Http Outbound

As promised I’m going to show a test case for which I have used Citrus to implement it. The case is like this:
In a predefined folder on a machine a character-separated file is placed. This file is picked up by our Mule ESB application. The content is transformed to XML and this XML document is posted to another application over HTTP. This application responds with XML document telling the document was processed correctly.
To test this interface with Citrus we have to perform the following steps:

  1. Copy the input test-file to the predefined folder so it is picked up by the test instance of our Mule ESB
  2. Our Mule ESB will poll a directory each second to check if there is placed a new file to process. Our test case starts by having Citrus putting a test file in the directory .

  3. Have Citrus listen on a predefined HTTP port to receive the XML message over HTTP
  4. Inside our Mule ESB the file is transformed to XML message and posted on a HTTP port to be processed by the ‘ORDER’ system. We have configured Citrus to listen on this specific HTTP port to pick up the XML message.

  5. Reply to the received message with a predefined XML message
  6. This steps mimics the ‘ORDER’ system as it would normally process the incoming XML message and respond with another XML message. In this case Citrus will send back a predefined XML message as response.

The citrus-config.xml for this test case can be found here. I will explain the content of the config file next:

<bean id="citrusJMSConnectionFactory"
        <property name="brokerURL" value="vm://localhost:61616" />
        <property name="userName" value="user"/>
        <property name="password" value="password"/>

This part defines a ‘in-memory’ JMS provider to be used by Citrus. We will see later how we use it to check the incoming HTTP request (as described here in the Citrus reference guide).

<bean id="namespaceContextBuilder" class="com.consol.citrus.xml.namespace.NamespaceContextBuilder">
        <property name="namespaceMappings">
                <prop key="ns1"></prop>

Here we define the namespaces that are used in the xml messages in our test. We will use this namespace in the test to check values of certain XML elements.

<bean name="pascalalmaIntegrationTests"
        <property name="tasksBefore">
                <bean class="com.consol.citrus.actions.ExecuteSQLAction">
                    <property name="dataSource" ref="rsDataSource"/>
                    <property name="statements">
                            <value>DELETE FROM RS_LOGS</value>

With this part I have Citrus clean up my log table in the database of my Mule ESB. We log all incoming and outgoing messages in our ESB to the database.

<!-- manadatory if XML files are used in a test -->
<bean id="xmlMessageValidator" class="com.consol.citrus.validation.xml.DomXmlMessageValidator"/>
    <bean id="schemaRepository"
        <property name="schemas">
                <bean class="org.springframework.xml.xsd.SimpleXsdSchema">
                    <property name="xsd"

When you want to work with XML messages in your test (and that is very likely nowadays) you will need to define an ‘xmlMessageValidator’. And to be able to validate the message against an XML schema you will need to define the schemas in the ‘schemaRepository’ as I have done here.

<bean id="rsDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="url" value="jdbc:oracle:thin:@test-vm:1521:XE"/>
        <property name="username" value="user"/>
        <property name="password" value="password"/>
        <property name="driverClassName" value="oracle.jdbc.xa.client.OracleXADataSource"/>
        <property name="removeAbandoned" value="true"/>

This is the datasource that Citrus can use to access the database that is used by our Mule ESB application. We use this datasource to clean up the logtable and can use it to perform validations after messages have been processed.

 <citrus:message-channel-sender id="invoiceFileSender" channel="fileOutboundChannel"/>
<file:outbound-channel-adapter id="invoice-files-out" channel="fileOutboundChannel"
        directory="file://Volumes/staging/invoice" filename-generator="test-filename-generator" />
<bean id="test-filename-generator" class="net.pascalalma.citrus.TestFileNameGenerator" />
<si:channel id="fileOutboundChannel" />

This is a mix of Citrus with spring-integration configuration. With this configuration I am able to send files to my test VM with Citrus. It also shows how easy it is to mix Citrus with spring-integration.

<!-- receive Invoice HTTP request -->
<citrus-http:server id="orderHttpServer"

Definition of the HTTP Server in Citrus which listens on port 8060 for a request of our Mule ESB.

 <bean id="jmsForwardingMessageHandler"
        <property name="destinationName" value="Citrus.order-response"/>
        <property name="connectionFactory" ref="citrusJMSConnectionFactory" />
        <property name="replyTimeout" value="6000"/>

    <!-- Process all received requests -->
    <citrus:jms-sync-message-receiver id="orderRequestReceiver"
                            connection-factory="citrusJMSConnectionFactory" />
    <citrus:jms-reply-message-sender id="orderResponseSender"
                            connection-factory="citrusJMSConnectionFactory" />

THis part forwards the incoming HTTP request to a JMS endpoint. In our test we can perform tests on this incoming JMS message and check if it is valid.

Next thing I want to explain is the test-config.xml itself. I think it is quite self-explainatory:

<?xml version="1.0" encoding="UTF-8"?>
<spring:beans xmlns=""
              xmlns:spring="" xmlns:xsi=""

    <testcase name="Testcase001Test">
            <author>Pascal Alma</author>

        <description>Tests Testcase001</description>

            <send with="invoiceFileSender">
                    <resource file="classpath:data/testcase001.txt"/>
            <receive with="orderRequestReceiver">

                <message schema-validation="true">
                    <validate path="//ns1:post-invoice/ns1:CompanyCode" value="1"/>
                    <validate path="string:count(//ns1:post-invoice/ns1:InvoiceLine/ns1:DocID)" value="3"/>
                    <element name="rs_address" value="order-purchase-invoice-queue" />
                    <message path="count(//ns1:post-invoice/ns1:InvoiceLine/ns1:DocID)" variable="${nrOfInvoiceLines}"/>

            <send with="orderResponseSender">
                    <resource file="classpath:data/response-testcase001.xml"/>

The first action that is done is sending a file to my Mule ESB. The next step is that I wait for the response from my ESB. When I received that I check the contents of XML message at some points and check if it is valid against the schema.
Then after echoing the number of invoice lines I have Citrus return the HTTP request with a fixed XML response so the Mule ESB ends this action successfully.
That completes this example of how to use Citrus to test your ESB implemention. I hope it is more clear now how you can use Citrus for your (automated) integration tests with this real life example.


About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Citrus, Mule, Spring Framework and tagged , , . Bookmark the permalink.

1 Response to Citrus testcase example: CSV File Inbound -> Xml Http Outbound

  1. Thank you for this excellent example on Citrus working with file and http adapters! We will definitely work on the documentation lacks you have mentioned and improve the configuration, so people get into the framework more easily.


Comments are closed.