arquillian测试框架具有普通测试框架无法做到的是功能,对容器依赖的EJB测试,当然这个测试过程是要比普通的测试要稍微复杂一些
此配置在官网有详细的示例,此处就不再重复,请参考http://arquillian.org/guides/
<profile> <id>dev</id> <activation> <activeByDefault>false</activeByDefault> </activation> <dependencies> <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa-all</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.1</version> <type>jar</type> <scope>provided</scope> </dependency> </dependencies> </profile>配置测试框架需要的基础jar包(亦可参照官网介绍)
<dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.arquillian</groupId> <artifactId>arquillian-bom</artifactId> <version>1.0.0.Final</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <!-- Arquillian JUnit --> <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <scope>test</scope> </dependency>
<profile> <id>arquillian-glassfish-embedded</id> <activation> <activeByDefault>true</activeByDefault> </activation> <dependencies> <dependency> <groupId>org.jboss.arquillian.container</groupId> <artifactId>arquillian-glassfish-embedded-3.1</artifactId> <version>1.0.0.CR3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.main.extras</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>3.1.2</version> <scope>provided</scope> </dependency> <!-- OpenJPA --> <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa-all</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>db2jcc</groupId> <artifactId>db2jcc</artifactId> <version>9.7</version> </dependency> <dependency> <groupId>db2jcc</groupId> <artifactId>db2jcc_license_cu</artifactId> <version>9.7</version> </dependency> </dependencies> </profile>
<?xml version="1.0" encoding="UTF-8"?> <arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd"> <container qualifier="glassfish-embedded" default="true"> <configuration> <property name="resourcesXml"> src/test/resources/resources-glassfish-embedded/glassfish-resources.xml </property> </configuration> </container> </arquillian>.../glassfish-resources.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"> <resources> <jdbc-resource pool-name="ArquillianEmbeddedH2Pool" jndi-name="jndi/name" /> <jdbc-connection-pool driver-classname="com.ibm.db2.jcc.DB2Driver" res-type="java.sql.Driver" name="ArquillianEmbeddedH2Pool"> <property name="URL" value="jdbc:db2://IP:Port/dbName:driverType=4;"></property> <property name="user" value="db2inst1"></property> <property name="password" value="yourPassword"></property> </jdbc-connection-pool> </resources>3、persistence.xml 普通的jap配置以外需要加上RuntimeUnenhancedClasses 支持
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="BASE" transaction-type="JTA"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <jta-data-source>jndi/name</jta-data-source> <properties> <property name="openjpa.RuntimeUnenhancedClasses" value="supported" /> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" /> </properties> </persistence-unit> </persistence>4、测试方法
@RunWith(Arquillian.class) public class TestServiceTest { @Deployment public static Archive<?> createDeployment() { WebArchive webInfResource = ShrinkWrap .create(WebArchive.class, "test.war") .addPackages(true, TestService.class.getPackage(), TestTable.class.getPackage()) .addAsResource("META-INF/persistence.xml") .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); System.out.println("webInfResource:" + webInfResource); return webInfResource; } @EJB private TestService ts; @Test public void test() { ts.add(); ts.select(); } }