---
schema_version: "1"
id: 15ac6eaa27dac8fa6e47ddd32762688431f9e801fb1e5881342bb82d08a64258
title: データベース
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/munit/3.0/db-util.md
canonical_url: https://docs.mulesoft.com/jp/munit/3.0/db-util
source_site: docs.mulesoft.com
content_type: documentation
content_hash: b99359c25c6a4a3754e310d019c77caa4f699779273976e21da0c8de91f24400
description: MuleSoft Documentation Site
release: "3.0"
latest: false
---
# データベース

データベースサーバーに接続するコードをテストするには、ローカル環境にデータベースサーバーをインストールしてテストを実行する必要があります。また、テスト専用の外部データベースサーバーを使用することもできますが、Maven プロジェクトは移植できません。サードパーティがテストデータベースサーバーをインストールしてプロジェクトをコンパイルする必要があります。

データベース接続を容易にテストできるように、MUnit ではローカル環境にデータベースサーバーを実装できます。

## [](#munit-db-server-module-をインストールする)MUnit DB Server Module をインストールする

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. データベースに使用される ​`h2`​ 連動関係は共有ライブラリ。  
    `pom.xml`​ ファイルに連動関係を追加し、​`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>
    ```

## [](#属性リファレンス)属性リファレンス

<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">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>name</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">この DB サーバーの設定名を定義します。この値は一意である必要があります。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>database</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">メモリ内 DB の名前を定義します。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>sqlFile</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">DB 構造/コンテンツを定義する SQL ファイルへのパスを指定します。このパラメーターは、起動時に DB を作成するために使用されます。詳細は、​<a href="#db-structure-sql">「SQL ファイルからの DB 構造の定義」</a>​セクションを参照してください。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>csv</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">DB 構造/コンテンツの定義に使用されるカンマ区切りファイルのパスを指定します。このパラメーターは、起動時に DB を作成するために使用されます。詳細は、​<a href="#db-structure-csv">「CSV ファイルからの DB 構造の定義」</a>​セクションを参照してください。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>connectionStringParameters</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">データベースへの接続文字列を定義します。セミコロンでパラメーターを連結できます。</p></td>
</tr>
</tbody>
</table>

## [](#db-構造の定義)DB 構造の定義

データベースの構造とコンテンツを定義するには、2 つの異なる方法があります。

- SQL
- CSV

MUnit DB サーバーは H2 に基づきます。複雑な構造、インデックス、および結合コマンドは適切に機能しない可能性があります。

### [](#db-structure-sql)SQL ファイルからの DB 構造の定義

SQL ファイルから DB の構造とコンテンツを定義するには、有効な ANSI SQL DDL (データ定義言語) 命令のセットを指定する必要があります。

```xml
<dbserver:config name="MUnit_DB_Server_Config" >
    <dbserver:connection sqlFile="FILE_NAME.sql" database="DATABASE_NAME" connectionStringParameters="MODE=MySQL" />
</dbserver:config>
```

### [](#db-structure-csv)CSV ファイルからの DB 構造の定義

CSV ファイルに基づく DB 構造は次のように定義します。

- テーブルの名前はファイルの名前 (以下の例では ​`customers`​)。
- 列の名前は CSV ファイルのヘッダー。

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

DB 構造を複数の CSV ファイルに分割することもできます。これを行うには、ファイル名を 1 つのリストにして、セミコロンで名前を区切ります。

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

これにより、​`FILE_NAME`​ と ​`FILE_NAME_1`​ の 2 つのテーブルが作成されます。

## [](#munit-db-サーバープロセッサー)MUnit DB サーバープロセッサー

DB サーバーユーティリティには、DB サーバーとのやり取りを行うための 2 つのプロセッサーがあります。

### [](#validate-that)Validate-That

MUnit DB サーバーでは、クエリの結果が期待どおりであることを検証できます。

これを行うには、​`validate-that`​ 操作を使用します。次のように、​`results`​ プロパティを改行文字 (​`\n`​) で区切られた行を含む CSV に設定します。

```xml
<dbserver:validate-that config-ref="DB_Server"
query="SELECT * FROM jobtitlelookup WHERE JOBTITLE='Developer';" returns="&quot;JOBTITLE&quot;,&quot;EECSALARYORHOURLY&quot;,&quot;JOBTITLEID&quot;\n&quot;Developer&quot;,&quot;10&quot;,&quot;DEV&quot;" />
```

### [](#execute)Execute

MUnit DB サーバーでは、メモリ内データベースで命令を実行できるため、テストの前にレジストリを追加または削除したり、データが適切に保存されたかどうかを確認したりできます。

`execute`​ 操作で DB Server Connector を使用して、テーブル (​`Culinary Team Member,10,HIR`​) に新しい値を挿入します。次に、​`execute query`​ 操作で別の DB Server Connector を使用して、テーブルからすべての値 (新しく追加された値を含む) を取得し、ペイロードを保存するためにロガーコンポーネントを使用してペイロードを確認します。

```xml
<!-- Execute a SQL instruction using the execute operation -->
<dbserver:execute config-ref="MUnit_DB_Server_Config" sql="INSERT INTO jobtitlelookup VALUES ('Culinary Team Member','10','HIR');"/>

<!-- Check the update using execute-query operation -->
<dbserver:execute-query config-ref="MUnit_DB_Server_Config" sql="SELECT * FROM jobtitlelookup"/>

<!-- log the resulting payload -->
<logger level="INFO" message="#[payload]"/>
```

コンソールのロガーは次のようになります。

```text
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: [{JOBTITLEID=DEV, EECSALARYORHOURLY=10, JOBTITLE=Developer}, {JOBTITLEID=HIR, EECSALARYORHOURLY=10, JOBTITLE=Culinary Team Member}]
```

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

- [MUnit テストでの DB サーバーユーティリティの設定](https://docs.mulesoft.com/jp/munit/3.0/db-server-cookbook)