Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Arquillianを使用したUnitTestコード
package jp.co.service;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import java.io.File;
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.GenericArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Test;
import org.junit.runner.RunWith;
import jp.co.persistence.entity.User;
@RunWith(Arquillian.class)
public class ServiceTest {
private static final String WEBAPP_SRC = "src/main/webapp";
@Deployment
public static Archive<?> createDeployment() {
WebArchive archive =
ShrinkWrap.create(WebArchive.class, "test.war")
//jp.coパッケージ配下のソースを全てパッケージにまとめる
.addPackages(true, "jp.co")
//xhtmlを全てパッケージにまとめる
.merge(ShrinkWrap.create(GenericArchive.class).as(ExplodedImporter.class)
.importDirectory(WEBAPP_SRC).as(GenericArchive.class),
"/", Filters.include(".*\\.xhtml$"))
//リソースファイルを追加する
     .setWebXML(new File("src/test/resources", "web.xml"))
.addAsWebInfResource(new File("src/test/resources", "faces-config.xml"))
.addAsResource(new File("src/test/resources/jp/co/persistence", "mybatis-config.xml"))
.addAsResource(new File("src/test/resources", "Messages_ja.properties"))
.addAsResource(new File("src/test/resources", "ValidationMessages_ja.properties"))
.addAsResource(new File("src/test/resources/jp/co/persistence/mapper", "TestMapper.xml"))
.addAsResource(new File("src/test/resources", "arquillian.xml"));
//pom.xmlに記載された依存ライブラリをパッケージングする
archive.addAsLibraries(
Maven.resolver()
.loadPomFromFile("pom.xml")
.importRuntimeDependencies()
.resolve()
.withTransitivity()
.asFile());
return archive;
}
@Inject
UserService userService;
@Test
public void selectIdTest() throws Exception {
// SetUp
// Exercise
User user = userService.selectId(10, "100");
// Verify
assertThat(mstUser, is(notNullValue()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment