JavaEE7プロジェクトでArquillianを使ってみる
TweetPosted on Saturday Jan 25, 2014 at 09:23PM in Technology
チュートリアルはJavaEE6ベースで書かれていたが、実際にはJavaEE7で作業したいのでJavaEE7で環境を作る方法
本当はArchetypeを作りたいのだが、まだMavenもEclipseも良くわかっていないのでまた後日
環境
- WildFly8.0.0.CR1
- Oracle JDK7u51
手順
- WildFly8.0.0CR1でサーブレットを動かしてみるの要領でjavaee7-essentials-archetypeでプロジェクトを作る
- java build pathを開き、test用のフォルダが欠けていたりする(test/resourcesとtest/java)のでそれらを作ってRefresh。test用のフォルダはOutput folderをtarget/test-classesに変更する。resourcesフォルダを作ったらExcludedを「**」にする
- プロジェクトの設定のdeployment assemblyを開いてテスト用の資源がデプロイされないようにする。test/resourcesがあれば削除
- java build pathのorder and exportで並び順を変更
- pom.xmlを編集する
pom.xml
こういう感じに改変してやれば良さげ。Arquillian Persistenceも入れておく
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>arquillian-javaee7</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.2.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-integration</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>net.avh4.util</groupId>
<artifactId>imagecomparison</artifactId>
<version>0.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-arquillian-container-remote</artifactId>
<version>8.0.0.CR1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-impl</artifactId>
<version>1.0.0.Alpha6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
こういう感じのテストが走るようになる
[1]にあったサンプルを少し改変したもの。test-persistence.xmlやjbossas-ds.xmlはArquillianチュートリアルのJPAを使ったテストを走らせるところをやってみるのと同じ。
package org.arquillian.example;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
import org.jboss.arquillian.persistence.UsingDataSet;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(Arquillian.class)
public class UserPersistenceTest {
@Deployment
public static Archive<?> createDeploymentPackage() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(UserAccount.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("jbossas-ds.xml");
}
@PersistenceContext
EntityManager em;
@Test
@UsingDataSet("datasets/users.yml")
@ShouldMatchDataSet("datasets/expected-users.yml")
public void should_change_user_password() throws Exception {
// given
String expectedPassword = "LexLuthor";
UserAccount user = em.find(UserAccount.class, 2L);
// when
user.setPassword("LexLuthor");
user = em.merge(user);
// then
Assert.assertEquals(expectedPassword, user.getPassword());
}
}
テストを走らせると、log4jの設定が無い、slf4jの実装が無い、と怒られるのでそれは適宜対応。
参考文献
Tags: test