MUnit テストでのデータベースサーバユーティリティの設定

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

<!-- 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​ ファイルには次のコンテンツが含まれています。

db.config=Database_Config

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

MUnit DB サーバモジュールのインストール

  1. Anypoint Studio から、​[Mule Palette (Mule パレット)]​ ビューに移動し、​[Search in Exchange…​ (Exchange 内を検索…​)]​ を見つけます。

  2. 検索バーで​「MUnit Utils Database Server」​ (MUnit ユーティリティデータベースサーバ) を検索し、そのモジュールをプロジェクトに追加します。

    <!-- 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​ としてリストします。

    <!--Third party libs-->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.166</version>
    </dependency>
    <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 サーバの定義

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

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

  1. プロジェクトの ​src/test/resources​ ディレクトリに移動します。

  2. 次の値を含む、​jobtitlelookup.csv​ という名前のファイルを作成します。

    JOBTITLE,EECSALARYORHOURLY,JOBTITLEID
    Developer,10,DEV
  3. [Global Elements (グローバル要素)]​ タブに移動し、​[MUnit DB Server Config (MUnit DB サーバ設定)]​ 要素を選択します。

  4. [Edit (編集)]​ をクリックし、次の項目に入力します。

    名前 MUnit_DB_Server_Config

    CSV

    jobtitlelookup.csv

    Database (データベース)

    DATABASE_NAME

    Connection string parameters (接続文字列パラメータ)

    MODE=MySQL

    <dbserver:config name="MUnit_DB_Server_Config" >
        <dbserver:connection csv="jobtitlelookup.csv" database="DATABASE_NAME" connectionStringParameters="MODE=MySQL" />
    </dbserver:config>
  5. DB サーバに接続する DB 設定を定義します。

    名前 Test_Database_Config

    Connection (接続)

    Generic connection

    URL

    jdbc:h2:tcp://localhost/mem:DATABASE_NAME

    Driver class name (ドライバクラス名)

    org.h2.Driver

    <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​ ファイルを定義します。

    db.config=Test_Database_Config

テストの実行

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

<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 サーバによって返されるペイロードが適切なものであることを検証します。