---
schema_version: "1"
id: fccb50dffb82be2f5edbd72313e37445ef03a78bba25ea96d4eb88c482e7604d
title: MUnit テストでのデータベースサーバーユーティリティの設定
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/munit/2.3/db-server-cookbook.md
canonical_url: https://docs.mulesoft.com/jp/munit/2.3/db-server-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: b69d8b927b8b1c649bbcc4c59dcd272d46896ac0278c1fb115bf73671a424bdf
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
# MUnit テストでのデータベースサーバーユーティリティの設定

次の Mule アプリケーションをテストするとします。

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

`src/main/resources`​ にある ​`db.properties`​ ファイルには次のコンテンツが含まれています。

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

次の手順を実行するテストを作成する必要があります。 \* `jobtitlelookup`​ テーブルでデータベースサーバーを含める。 \* データベースコネクタがクエリを実行するために有効なデータベース構造を提供する。 \* データベースコネクタが使用する ​`jobid`​ 変数の値を渡す。

## [](#munit-db-サーバーモジュールのインストール)MUnit DB サーバーモジュールのインストール

1. Anypoint Studio から、​**\[Mule Palette (Mule パレット)\]**​ ビューに移動し、​**\[Search in Exchange…​ (Exchange 内を検索…​)\]**​ を見つけます。
2. 検索バーで​**「MUnit Utils Database Server」**​ (MUnit ユーティリティデータベースサーバー) を検索し、そのモジュールをプロジェクトに追加します。
    
    ```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>
    ```
    
    POM ファイル内の MUnit DB サーバーアーティファクトには ​`test`​ スコープが必要です。
3. POM ファイルに ​`h2`​ 連動関係を追加し、​`mule-maven-plugin`​ で ​`sharedLibrary`​ としてリストします。
    
    ```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>
    ```

## [](#munit-db-サーバーの定義)MUnit DB サーバーの定義

キャンバスの ​**\[Global Elements (グローバル要素)\]**​ から ​**\[MUnit DB Server Config (MUnit DB サーバー設定)\]**​ を使用して、データベースサーバーを定義します。

この例では、CSV ファイルを使用して DB 構造とコンテンツを定義します。

1. プロジェクトの ​`src/test/resources`​ ディレクトリに移動します。
2. 次の値を含む、​`jobtitlelookup.csv`​ という名前のファイルを作成します。
    
    ```text
    JOBTITLE,EECSALARYORHOURLY,JOBTITLEID
    Developer,10,DEV
    ```
3. **\[Global Elements (グローバル要素)\]**​ タブに移動し、​**\[MUnit DB Server Config (MUnit DB サーバー設定)\]**​ 要素を選択します。
4. **\[Edit (編集)\]**​ をクリックし、次の項目に入力します。
    
    <table class="tableblock frame-all grid-all fit-content spread">
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">名前</th>
<th class="tableblock halign-left valign-top">​<code>MUnit_DB_Server_Config</code>​</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>CSV</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>​<code>jobtitlelookup.csv</code>​</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Database (データベース)</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>​<code>DATABASE_NAME</code>​</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Connection string parameters (接続文字列パラメーター)</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<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. DB サーバーに接続する DB 設定を定義します。
    
    <table class="tableblock frame-all grid-all fit-content spread">
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">名前</th>
<th class="tableblock halign-left valign-top">​<code>Test_Database_Config</code>​</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Connection (接続)</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>​<code>Generic connection</code>​</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>URL</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>​<code>jdbc:h2:tcp://localhost/mem:DATABASE_NAME</code>​</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Driver class name (ドライバークラス名)</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<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. テストデータベース設定を取得する ​`src/test/resources`​ フォルダー内の ​`db.properties`​ ファイルを定義します。
    
    ```none
    db.config=Test_Database_Config
    ```

## [](#テストの実行)テストの実行

DB サーバーをインストールして設定したら、テストを実行できます。

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

このテストは、本番コードで実行されるクエリが正しいこと、および DB サーバーによって返されるペイロードが適切なものであることを検証します。

## [](#関連情報)関連情報

- [データベースユーティリティ](https://docs.mulesoft.com/jp/munit/2.3/db-util)