Arquillian を使用して Java EE GlassFish のテストをする(Getting Started)

arquillian.org

Arquillian の公式サイトを見ると日本語の情報が増えている。
今まで適当な理解でやっていたので、読み込んでいく。
すごく丁寧に書かれている。

まとめると、Arquillianを使うには、(JUnitと共に)以下の3つのライブラリが必要です:

1. Arquillian JUnit インテグレーション
2. ターゲットコンテナ向けのArquillianコンテナアダプタ
3. コンテナのランタイム(エンベデッドコンテナ向け)または、コンテナのクライアント(リモートコンテナ向け)

上記GlassFish で関連付けすると以下になるのかな。

Arquillian JUnit インテグレーション
<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <scope>test</scope>
</dependency>
ターゲットコンテナ向けのArquillianコンテナアダプタ
<dependency>
    <groupId>org.jboss.arquillian.container</groupId>
    <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
    <version>1.0.0.CR4</version>
    <scope>test</scope>
</dependency>
コンテナのランタイム(エンベデッドコンテナ向け)
<dependency>
    <groupId>org.glassfish.main.extras</groupId>
    <artifactId>glassfish-embedded-all</artifactId>
    <version>3.1.2</version>
    <scope>provided</scope>
</dependency>

以下を pom.xml に追加すると GlassFish 組み込みサーバー でテスト実行が可能になる。
plugin は必須ではないけど、まぁいいか。

pom.xml に以下を追加

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.1.15.Final</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<profiles>
    <profile>
        <id>arquillian-glassfish-embedded</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian.container</groupId>
                <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
                <version>1.0.0.CR4</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish.main.extras</groupId>
                <artifactId>glassfish-embedded-all</artifactId>
                <version>3.1.2</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>