Last active
December 26, 2016 04:34
-
-
Save sabburo/70ed2f401b3023085898065e587ce985 to your computer and use it in GitHub Desktop.
Arquillianを使用したUnitTestコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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