Configuring Database Server Utility in an MUnit Test

Assume that you want to test the following Mule application:

<!-- Local Database Configuration -->
<db:config name="Database_Config">
    <db:my-sql-connection host="localhost" port="1234" />
<!-- Properties according to the environment -->
<configuration-properties file="" />

<flow name="selectFlow" >
    <!-- Perform a query based on a value passed by a variable -->
    <db:select config-ref="${db.config}">
        <db:sql >SELECT * FROM jobtitlelookup WHERE jobtitleid = :id</db:sql>
        <db:input-parameters ><![CDATA[#[id : vars.jobid]]]></db:input-parameters>
    <!-- Set two conditions depending on the query result -->
        <!-- If there is one or more values resulting from the query set those values as the payload -->
        <when expression="#[sizeOf(payload) > 0]">
            <set-payload value="#[payload[0].jobtitle]"/>
        <!-- If the query throws no results, inform it in the payload  -->
            <set-payload value="#['No job title for $(vars.jobid) was found']" />

    <!-- Finally, pass the payload content as a Flow variable  -->
    <set-variable value="#[payload]" variableName="job" />

The file in src/main/resources has the following content:


You need to create a test that: * Contains a database server with a jobtitlelookup table. * Provides a valid database structure for the database connector to perform its query. * Passes a value for the jobid variable that Database Connector uses.

Install the MUnit DB Server Module

  1. From Anypoint Studio, go to the Mule Palette view and locate Search in Exchange…​.

  2. In the search bar, look for MUnit Utils Database Server and add the module to your project:

    <!-- dbserver Dependency -->

    The MUnit DB server artifact in your POM file must have the test scope.

  3. Add the h2 dependency to your POM file and list it as sharedLibrary in mule-maven-plugin:

    <!--Third party libs-->

Define the MUnit DB Server

Define the database server using the MUnit DB Server Config from the Global Elements in your canvas.

For this example, define the DB structure and content using a CSV file:

  1. Go to the src/test/resources directory in your project.

  2. Create a file named jobtitlelookup.csv containing the following values:

  3. Navigate to the Global Elements tab and select the MUnit DB Server Config element.

  4. Click Edit and complete the fields:

    Name MUnit_DB_Server_Config





    Connection string parameters


    <dbserver:config name="MUnit_DB_Server_Config" >
        <dbserver:connection csv="jobtitlelookup.csv" database="DATABASE_NAME" connectionStringParameters="MODE=MySQL" />
  5. Define the DB configuration to connect to your DB server:

    Name Test_Database_Config


    Generic connection



    Driver class name


    <db:config name="Test_Database_Config">
        <db:generic-connection url="jdbc:h2:tcp://localhost/mem:DATABASE_NAME" driverClassName="org.h2.Driver" />
  6. Define the file in your src/test/resources folder that will pick up your test database configuration:


Run the Test

After installing and configuring the DB server you can run the test:

<munit:test name="selectFlowTest" description="Test selectFlow"  >
        <!-- Passes a variable to value to run in the main flow. -->
        <set-variable variableName="jobid" value="DEV" />
        <!-- Run the production code. -->
        <flow-ref name="db-server-docsFlow"/>
        <munit-tools:assert-equals actual="#[vars.job]" expected="Developer" />

This test validates that the query run in your production code is correct and that the payload returned by the DB server is as expected.

See Also

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Give us your feedback!
We want to build the best documentation experience for you!
Help us improve with your feedback.
Take the survey!