LoginSignup
1

More than 5 years have passed since last update.

posted at

updated at

wildfly-maven-pluginを使ったintegration test

概要

integration testをWildFlyを使っておこなう。
maven failsafe pluginでintegration-testゴールを追加し、直前にWildFlyの起動、デプロイ、直後にアンデプロイ、停止処理を実施する。

環境

  • WildFly 8.0.0.Final

内容

pom.xmlにfailsafe pluginとwildfly pluginを追加する。pre-integration-test、post-integration-testでWildFlyを制御する。

pom.xml
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.17</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>            
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>1.0.1.Final</version>
                <configuration>
                    <jbossHome>${wildflyPath}</jbossHome>
                    <serverConfig>standalone.xml</serverConfig>
                </configuration>
                <executions>
                    <execution>
                        <id>start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>deploy</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>undeploy</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>undeploy</goal>
                        </goals>
                    </execution>          
                    <execution>
                        <id>shutdown</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>shutdown</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

<jbossHome>${wildflyPath}</jbossHome>ではWildFlyのインストールディレクトリを指定している。

${wildflyPath}はmavenのsettings.xmlに記載。

~/.m2/settings.xml
  <profiles>
    <profile>
      <id>alwaysActiveProfile</id>
      <properties>
        <wildflyPath>/home/xxx/dev/wildfly-8.0.0.Final</wildflyPath>
      </properties>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
  </activeProfiles>

テストコードからはjersey clientでWildFlyにデプロイされているアプリケーションのWeb APIを実行する。今回は既存プロジェクトを想定しているため、jersey 1.xを使用する。

テストに必要なモジュールをpomに追加。

pom.xml
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>2.4.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.18.1</version>
            <scope>test</scope>
        </dependency>        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

テストソースはファイル名の末尾をITにしておく(*IT.java)とintegration-testで実行される。

APIを呼んで200OKが返るだけのテストコードを追加。

public class PingIT {

    public PingIT() {


    }

    @Test
    public void pingTest() {
       ClientResponse res = null;
        try {
       Client client = Client.create();
       WebResource webResource = client.resource("http://localhost:8080/testapp/xxx");
       res = webResource.get(ClientResponse.class);
        } catch(Error e) {
            fail();
        }       
       assertEquals(ClientResponse.Status.OK.getStatusCode(), res.getStatus());

    }

}

後は、mvn integration-test で実行するだけ。

その他

pomにjersey-clientだけを追加し、jersey-commonを追加していないとClientインスタンスを生成するときに、java.lang.ExceptionInInitializerError が発生した。エラーの原因は、以下のクラスが見つからないらしい。

java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl

GlassFishを使っているときは、こんなエラーは出なかったような。。。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1