---
schema_version: "1"
id: 50965102f6cbf13cc5a72381b0884250212dcf216218036c13c7bb1834ecae86
title: Configuring the 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/3.6/db-server-cookbook.md
canonical_url: https://docs.mulesoft.com/munit/3.6/db-server-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 4cd45d0078aed3983414145569c3d401a77cd12d68c4fbcca2e4f9c3d00cb631
description: MuleSoft Documentation Site
release: "3.6"
latest: false
---
---
product: MUnit
version: 3.6
---

# Configuring the Database Server Utility in an MUnit Test

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

![logo acb active](/mulesoft/current/en-us/_/img/icons/anypoint-code-builder.svg "Anypoint Code Builder")Anypoint Code
Builder

![logo studio active](/mulesoft/current/en-us/_/img/icons/studio.svg "Anypoint Studio")Anypoint Studio

If 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 must 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.  Look for the **MUnit Utils Database Server** module.

2.  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>
    ```

The **h2** dependency enhances MUnit tests by allowing the simulation of database interactions using
an in-memory, lightweight, and fast database. This setup ensures tests are both isolated and
repeatable.

> [!NOTE] The **MUnit DB Server** module is located at
> [MuleSoft Nexus repository](https://repository.mulesoft.org/nexus/content/repositories/releases/com/mulesoft/munit/utils/munit-dbserver-module/).
> The **MUnit DB Server** module version 3 and later is compatible with Java 17. Previous versions
> of this module available at Anypoint Exchange are deprecated.

## Define the MUnit DB Server

Define the database server by creating the **global-configs.xml** file and defining the MUnit DB
Server Config element.

Define the MUnit DB Server Config element as follows:

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.  Create the **global-configs.xml** file and define the **MUnit DB Server Config** element as
    follows:

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

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

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

5.  Define the `db.properties` file in your `src/test/resources` folder that picks 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 DB server
returns the payload as expected.

## See Also

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

- [Add Modules to Your Test in Anypoint Code Builder](https://docs.mulesoft.com/anypoint-code-builder/ref-acb-commands)

- [Add Modules to Your Test in Anypoint Studio](https://docs.mulesoft.com/studio/add-modules-in-studio-to)
