---
schema_version: "1"
id: 302fbeb35e0e4de1fbb4ae66e736139d86f69cc48b4dec9d7385721ef672964d
title: Configuring Database Server Utility in an MUnit Test
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/munit/2.3/db-server-cookbook.md
canonical_url: https://docs.mulesoft.com/munit/2.3/db-server-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d997aec71e3484b4f6c653bd6744a5ec435d38e3bb7337ff567cb37f59e9500d
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: MUnit
version: 2.3
---

# Configuring Database Server Utility in an MUnit Test

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Assume that you want to test the following Mule application:

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

<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>
    </db:select>
    <!-- Set two conditions depending on the query result -->
    <choice>
        <!-- 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]"/>
        </when>
        <!-- If the query throws no results, inform it in the payload  -->
        <otherwise>
            <set-payload value="#['No job title for $(vars.jobid) was found']" />
        </otherwise>
    </choice>

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

The file `db.properties` in `src/main/resources` has the following content:

```none
db.config=Database_Config
```

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:

    ```xml
    <!-- dbserver Dependency -->
    <dependency>
        <groupId>com.mulesoft.munit.utils</groupId>
        <artifactId>munit-dbserver-module</artifactId>
        <version>2.0.2</version>
        <classifier>mule-plugin</classifier>
        <scope>test</scope>
    </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`:

    ```xml
    <!--Third party libs-->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.166</version>
    </dependency>
    ```

    ```xml
    <plugin>
        <groupId>org.mule.tools.maven</groupId>
        <artifactId>mule-maven-plugin</artifactId>
        <version>${mule.maven.plugin.version}</version>
        <extensions>true</extensions>
        <configuration>
        <sharedLibraries>
                ...
                <sharedLibrary>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                </sharedLibrary>
            </sharedLibraries>
        </configuration>
    </plugin>
    ```

## 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:

    ```text
    JOBTITLE,EECSALARYORHOURLY,JOBTITLEID
    Developer,10,DEV
    ```

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

4.  Click **Edit** and complete the fields:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th><code>MUnit_DB_Server_Config</code></th></tr></thead><tbody><tr><td><div><div><p>CSV</p></div></div></td><td><div><div><p><code>jobtitlelookup.csv</code></p></div></div></td></tr><tr><td><div><div><p>Database</p></div></div></td><td><div><div><p><code>DATABASE_NAME</code></p></div></div></td></tr><tr><td><div><div><p>Connection string parameters</p></div></div></td><td><div><div><p><code>MODE=MySQL</code></p></div></div></td></tr></tbody></table>

    ```xml
    <dbserver:config name="MUnit_DB_Server_Config" >
        <dbserver:connection csv="jobtitlelookup.csv" database="DATABASE_NAME" connectionStringParameters="MODE=MySQL" />
    </dbserver:config>
    ```

5.  Define the DB configuration to connect to your DB server:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th><code>Test_Database_Config</code></th></tr></thead><tbody><tr><td><div><div><p>Connection</p></div></div></td><td><div><div><p><code>Generic connection</code></p></div></div></td></tr><tr><td><div><div><p>URL</p></div></div></td><td><div><div><p><code>jdbc:h2:tcp://localhost/mem:DATABASE_NAME</code></p></div></div></td></tr><tr><td><div><div><p>Driver class name</p></div></div></td><td><div><div><p><code>org.h2.Driver</code></p></div></div></td></tr></tbody></table>

    ```xml
    <db:config name="Test_Database_Config">
        <db:generic-connection url="jdbc:h2:tcp://localhost/mem:DATABASE_NAME" driverClassName="org.h2.Driver" />
    </db:config>
    ```

6.  Define the `db.properties` file in your `src/test/resources` folder that will pick up your test
    database configuration:

    ```none
    db.config=Test_Database_Config
    ```

## Run the Test

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

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

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

- [Database Util](https://docs.mulesoft.com/munit/2.3/db-util)
