本系列教程共五篇,分别是:
一、 环境搭建、容器适配、单元测试
二、JBoss Forge、ShrinkWrap快速指南
三、使用 Arquillian 进行Java持久化测试
四、使用Arquillian + Drone + Selenium + Graphene 进行Web自动化测试
五、使用 Arquillian 在云端进行测试
其中,Web容器以JBoss-as-7.1.1为例,工程以maven目录结构为准。
本文所涉及到的内容有:
1. Arquillian
2. Maven
3. JUnit
4. Seam Framework
5. ShrinkWrap
6. JBoss Forge
forge
new-project --named arquillian-demo --topLevelPackage cn.demo
<modelVersion>4.0.0</modelVersion> <groupId>cn.demo</groupId> <artifactId>arquillian-demo</artifactId> <version>1.0.0-SNAPSHOT</version> <repositories> <repository> <id>JBOSS_NEXUS</id> <url>http://repository.jboss.org/nexus/content/groups/public</url> </repository> </repositories> <build> <finalName>arquillian-demo</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
<modelVersion>4.0.0</modelVersion> <groupId>cn.demo</groupId> <artifactId>arquillian-demo</artifactId> <version>1.0.0-SNAPSHOT</version> <repositories> <repository> <id>JBOSS_NEXUS</id> <url>http://repository.jboss.org/nexus/content/groups/public</url> </repository> </repositories> <build> <finalName>arquillian-demo</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> <!-- newly add starts --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.arquillian</groupId> <artifactId>arquillian-bom</artifactId> <version>1.1.3.Final</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <!-- newly add ends -->
package cn.demo; public class Hello { public String sayHello(String name) { return "hello," + name; } }
package cn.demo; import javax.inject.Inject; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(Arquillian.class) public class HelloTest { @Inject private Hello helloBean; // 注入一个Hello对象 @Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class).addClass(Hello.class) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } @Test public void isHelloValid() { Assert.assertNotNull(helloBean); // 判断注入是否成功 Assert.assertEquals("hello,Neo", helloBean.sayHello("Neo")); // 判断返回值是否正确 } }
<profiles> <profile> <id>arq-jbossas-remote</id> <dependencies> <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-6.0</artifactId> <version>1.0.0.Final</version> <type>pom</type> <scope>provided</scope> </dependency> <dependency> <groupId>org.jboss.as</groupId> <artifactId>jboss-as-arquillian-container-remote</artifactId> <version>7.1.1.Final</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.protocol</groupId> <artifactId>arquillian-protocol-servlet</artifactId> <scope>test</scope> </dependency> </dependencies> </profile> </profiles>
mvn test -Parq-jbossas-remote
mvn test -Parq-glassfish-remote
mvn clean install -Dmaven.test.skip=true
mvn test -Parq-jbossas-remote
<profile> <id>arquillian-jbossas-managed</id> <dependencies> <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-6.0</artifactId> <version>1.0.0.Final</version> <type>pom</type> <scope>provided</scope> </dependency> <dependency> <groupId>org.jboss.as</groupId> <artifactId>jboss-as-arquillian-container-managed</artifactId> <!-- 注意这里换成了managed而不是remote --> <version>7.1.1.Final</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.protocol</groupId> <artifactId>arquillian-protocol-servlet</artifactId> <scope>test</scope> </dependency> </dependencies> </profile>
<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="jbossas-managed" default="true"> <configuration> <property name="jbossHome">你的JBoss安装目录</property> </configuration> </container> </arquillian>
mvn test -Parquillian-jbossas-managed
mvn test -P你起的profile名