Search code examples
maventestingmuleintegrationmunit

Post Deployment Integration Testing in Mule


I have an application for which I have an Integration Test suite written using Munit. I am deploying this to CloudHub Using Jenkins.

How can I execute the test cases post-deployment?

Is there any command line tool that can I make use of or it can done using maven or Jenkins?


Solution

  • You can configure your Maven build to deploy your Mule application during the pre-integration-test phase, run your tests during the integration-test phase and optionally undeploy during the post-integration-test phase. You can use something like:

    <plugins>
    
        ...
    
        <plugin>
            <groupId>org.mule.tools.maven</groupId>
            <artifactId>mule-app-maven-plugin</artifactId>
            <version>1.1</version>
            <extensions>true</extensions>
        </plugin>
        <plugin>
            <groupId>org.mule.tools.maven</groupId>
            <artifactId>mule-maven-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <deploymentType>cloudhub</deploymentType>
                <!-- muleVersion is the runtime version as it appears on the CloudHub interface -->
                <muleVersion>3.7.0</muleVersion>
                <username>myUsername</username>
                <password>myPassword</password>
                <redeploy>true</redeploy>
                <environment>Production</environment>
            </configuration>
            <executions>
                <execution>
                    <id>deploy</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
                <execution>
                    <id>undeploy</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>undeploy</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
                <groupId>com.mulesoft.munit.tools</groupId>
                <artifactId>munit-maven-plugin</artifactId>
                <version>${munit.version}</version>
                <executions>
                    <execution>
                        <id>unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <munittest>.*-unit-test-suite.xml</munittest>
                        </configuration>
                    </execution>
                    <execution>
                        <id>it-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <munittest>.*-integration-test-suite.xml</munittest>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <coverage>
                        <runCoverage>false</runCoverage>>
                    </coverage>
                </configuration>
            </plugin>
    
        ...
    
    </plugins>
    

    See the Mule Maven plugin documentation for details on how to configure the deployment on CloudHub.

    EDIT: as you are using MUnit to run your tests, you will have to configure the MUnit Maven Plugin to run your integration tests, taking care of differentiating them from eventual Unit Tests. See MUnit Maven Support. Your MUnit Integration Tests should run on the integration-test phase. If you have trouble configuring your build let me know in the comment I'll edit accordingly.

    EDIT2: I updated my answer to provide a working example of MUnit Maven configuration able to perform both unit-testing and integration-testing.

    There are 2 executions configured:

    • The first one will run on test phase and use only MUnit tests matching the .*-unit-test-suite.xml regex (via the munittest parameter)
    • The second one will run on integration-test and use only tests matching the .*-integration-test-suite.xml regex.

    You'll then have to name your Unit Tests and Integration Tests according to these patterns to make sure they are launched in the proper order. That's an example of course, the important thing is making sure your Unit Tests and Integration Tests are differentiated and launched at the proper moment - as it is done with the Maven Failsafe and Surefire Plugin using respectively *Test and *IT classes.

    If you only have Integration Tests to run, you can skip this complicated configuration and just use the integration-test execution without the munittest parameter.

    In a nutshell, your build should then do something like:

    1. Run Unit Tests via MUnit (unit-test execution during test phase)
    2. Deploy your application on Cloudhub (deploy execution during pre-integration-test phase
    3. Run Integration Tests via MUnit (it-test execution during integration-test phase)
    4. Undeploy your application from Cloudhub (undeploy execution during post-integration-test phase

    If your are not familiar with phases and executions, read Introduction to the Build Lifecycle