---
schema_version: "1"
id: 580b31b08d2c79cac1b61741cb5039c1007442638db5014efa06e650965a8297
title: 最初のテストケースの作成
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/mule-sdk/1.1/testing-writing-your-first-test-case.md
canonical_url: https://docs.mulesoft.com/jp/mule-sdk/1.1/testing-writing-your-first-test-case
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 95a30cc6d45fde135a73a996bafc930d307e6c72d01b24d05bd2dd88508e2dd7
description: MuleSoft Documentation Site
release: "1.1"
latest: false
---
# 最初のテストケースの作成

このセクションでは、モジュールの操作をテストするための、ステップごとの手順について説明します。また、実行する必要があるテストケースの基本について説明します。

## [](#1-つ目の概念)1 つ目の概念

モジュールのすべてのテストケースに以下の基本項目が含まれている必要があります。

- テストケースクラス: テストロジックを含む Java クラス。フローを実行し、結果についてアサーションを実行します。
- Mule アプリケーション XML: テストするモジュールを使用する Mule アプリケーション。

このドキュメントは、Mule 拡張アーキタイプを使用して生成されたモジュールプロジェクトに基づきます (​[最初の SDK プロジェクトの作成](https://docs.mulesoft.com/jp/mule-sdk/1.1/getting-started#generating-a-project-using-the-maven-archetype-directly)​)。 この例を使用してモジュールのテスト方法を学習し、親の連動関係については上記のドキュメントを参照します。このドキュメントには、テストケースの構築に必要なすべての連動関係が記載されています。

## [](#1-テストケースクラスの作成)1.テストケースクラスの作成

テストケースクラスは、​`MuleArtifactFunctionalTestCase`​ から継承される必要があります。これで、Mule アプリケーション内でのモジュールのテストが可能になります。

```java
public class BasicOperationsTestCase extends MuleArtifactFunctionalTestCase {

}
```

## [](#2-mule-アプリケーションの記述)2.Mule アプリケーションの記述

モジュールをテストするには、そのモジュールを使用する Mule アプリケーションを実行し、フローを実行して、実行結果についてアサーションを実行します。

Mule アプリケーション XML は、モジュールプロジェクトの ​`src/test/resources`​ フォルダー内に配置する必要があります。

```xml
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:basic="http://www.mulesoft.org/schema/mule/basic"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
          http://www.mulesoft.org/schema/mule/basic http://www.mulesoft.org/schema/mule/basic/current/mule-basic.xsd">

    <basic:config name="basic-config" configId="configId">
        <basic:connection requiredParameter="aValue" />
    </basic:config>

    <flow name="sayHiFlow">
        <basic:say-hi person="Mariano Gonzales"/>
    </flow>

    <flow name="retrieveInfoFlow">
        <basic:retrieve-info config-ref="basic-config"/>
    </flow>

</mule>
```

![first test app](/mulesoft/current/ja-jp/mule-sdk/1.1/_images/first-test-app.png)

ヒント: Mule アプリケーションの記述は、Anypoint Studio で Mule アプリケーションを作成し、生成された XML をコピーしてモジュールプロジェクト内に貼り付けることで簡単にできます。

## [](#3-テストケースへの-mule-アプリケーションのインポート)3.テストケースへの Mule アプリケーションのインポート

テストケースクラスと Mule アプリケーション XML を作成したら、その 2 つのコンポーネントをバインドし、テストケースを実行する Mule アプリケーションを識別します。これを行うには、​`getConfigFile()`​ メソッドを上書きし、Mule アプリケーション XML の相対パスを含む ​`String`​ を返します。

<table><tbody><tr><td class="icon"><i class="fa icon-warning" title="Warning"></i></td><td class="content">設定ファイルのパスは、​<code>module-project/src/test/resources</code>​ フォルダーを基準とする相対パスです。たとえば、XML ファイルが ​<code>module-project/src/test/resources/test-connector-mule-app.xml</code>​ に配置されている場合、​<code>getConfigFile()</code>​ メソッドは ​<code>test-connector-mule-app.xml</code>​ を返します。</td></tr></tbody></table>

```java
public class ModuleTestCase extends MuleArtifactFunctionalTestCase {

    @Override
    protected String getConfigFile() {
        return "test-connector-mule-app.xml";
    }

}
```

## [](#4-最初のテストケースの作成)4.最初のテストケースの作成

このフェーズでは、モジュールテストケースの作成を開始するためのすべてをセットアップします。最初のテストでは、フローを実行し、出力を取得して、それに対してアサーションを実行する簡単な操作をテストするように記述します。

### [](#testing-flowrunner)4.1 フローの実行

`flowRunner("flowName")`​ ユーティリティメソッドを使用して、​`FlowRunner`​ (フローを実行できるユーティリティ) のインスタンスを作成します。パラメーターで識別されたフローを実行するように ​`FlowRunner`​ インスタンスを設定します。

次に ​`FlowRunner`​ インスタンスで ​`run()`​ を実行します。次に例を示します。

```java
Event event = flowRunner("sayHiFlow").run();
```

フローを実行すると、​`payload`​、​`attributes`​、​`vars`​ と、フローの実行に関して使用可能なすべての情報を含む ​`Event`​ が返されます。

<table>
<tbody><tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>フローからストリームが返された場合は、keepStreamsOpen() メソッドをコールして、ランタイムによるストリームの終了 (フローの終了時の正常な動作です) を防止してください。keepStreamsOpen() メソッドをコールしないと、ストリームをコンシュームしようとしたときに ​<code>CursorProviderAlreadyClosedException</code>​ エラーが返されます。次の例は、keepStreamsOpen() メソッドのコール方法を示しています。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">Event event = flowRunner("streamFlow").keepStreamsOpen().run();</code></pre>
</div>
</div>
</td>
</tr>
</tbody></table>

### [](#4-2-ペイロード値の取得)4.2 ペイロード値の取得

フローを実行したら、​`event`​ 変数にフローの実行結果が含まれます。ペイロードの値を取得し、必要なアサーションを実行するには、この変数が必要です。

```java
    Object payloadValue = event.getMessage()
                               .getPayload()
                               .getValue();
    assertThat(payloadValue, is("Hello Mariano Gonzales!!!"))
```

### [](#4-3-まとめ)4.3 まとめ

次の例は、前述のテストケース手順をまとめたものです。

```java
@Test
public void executeSayHiOperation() throws Exception {
  Event sayHiFlow = flowRunner("sayHiFlow").run();           (1)
  String payloadValue = ((String) sayHiFlow
                                    .getMessage()
                                    .getPayload()
                                    .getValue());            (2)
  assertThat(payloadValue, is("Hello Mariano Gonzales!!!")); (3)
}
```

<table><tbody><tr><td><i class="conum" data-value="1"></i><b>1</b></td><td><code>sayHiFlow</code>​ フローを実行します。</td></tr><tr><td><i class="conum" data-value="2"></i><b>2</b></td><td>ペイロード値を取得します。</td></tr><tr><td><i class="conum" data-value="3"></i><b>3</b></td><td>値に対してアサーションを実行します。</td></tr></tbody></table>