jetty-maven-plugin+selenium-maven-pluginでintegration-test
integration-testを行う際に、jetty-maven-pluginでWebシステムをJettyで動かし、それをSeleniumを使ってテストしたい場合、それの一連動作を一括でやる方法。まぁ色々調べてたら(ggrks)、結構情報はあったのでメモしとく
pom.xml
以前やったSAStrutsなプロジェクトをそのまま利用する。でpom.xmlを以下のような感じで設定
<?xml version="1.0" ?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.kinjouj.sastruts</groupId>
<artifactId>kinjouj_sastruts</artifactId>
<version>1.0</version>
<name>kinjouj_sasatruts</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<contextPath>/</contextPath>
<systemProperties>
<systemProperty>
<name>log4j.configurationFile</name>
<value>file:${project.basedir}/src/main/resources/log4j.properties</value>
</systemProperty>
</systemProperties>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start-server</goal>
</goals>
<configuration>
<background>true</background>
<logOutput>true</logOutput>
<multiWindow>true</multiWindow>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-server</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<!-- integration-testなクラスはパッケージ分けた方がいいかも -->
<include>**/*TestCase.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>maven.seasar.org</id>
<name>The Seasar Foundation Maven2 Repository</name>
<url>http://maven.seasar.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.seasar.sastruts</groupId>
<artifactId>sa-struts</artifactId>
<version>1.0.4-sp9</version>
</dependency>
<dependency>
<groupId>org.seasar.container</groupId>
<artifactId>s2jdbc-gen</artifactId>
<version>2.4.46</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>toplink-essentials</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ejb_3.0_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-annotation_1.0_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_3.0_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_3.0_spec</artifactId>
<version>1.0</version>
<scope>war</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.26.0</version>
</dependency>
</dependencies>
</project>
surefire-pluginなテストな設定をしないと起動順序がおかしくなる模様
src/test/java/SampleTestCase.java
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import static org.junit.Assert.*;
public class SampleTestCase {
private Selenium selenium;
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium(
"localhost",
4444,
"*chrome",
"http://localhost:8080"
);
selenium.start();
}
@Test
public void testIndex() {
selenium.open("/");
assertEquals("test", selenium.getTitle());
}
@After
public void tearDown() {
selenium.stop();
}
}
っていうか
import org.junit.AfterClass;
import org.junit.BeforeClass;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class SeleniumTestCase {
protected static Selenium selenium;
@BeforeClass
public static void setUp() throws Exception {
selenium = new DefaultSelenium(
"localhost",
4444,
"*chrome",
"http://localhost:8080"
);
selenium.start();
}
@AfterClass
public static void tearDown() {
selenium.stop();
}
}
な感じで作っといてテストケースで継承して使うみたいな方式の方が良いでないかと。で難アリな所としてテストケース毎にSeleniumが起動するので若干テストの速度的にはあんまりよろしくは無いかも知れない。まぁ大規模開発とかになるとマジで厳しいかも
とりまぁ、そんな感じで別途サーバー起動してなくてもテストしたりする事が可能な模様という事で