arquillian-remote 进行EJB测试

一、使用默认的jpa实现进行remote测试

此配置请参照官网的配置http://arquillian.org/guides/getting_started_rinse_and_repeat/,此处不做重复。

二、使用自己的jpa实现进行remote测试

1、配置pom.xml
由于此测试框架有自己的一套JavaEE的实现jar包,所以会造成与工程内部的jsf和javaee6 api等冲突,使用测试框架时不能启用此jar包(通过配置profile,可参考arquillian官网示例介绍)。
<profile>
	<id>dev</id>
	<activation>
		<activeByDefault>true</activeByDefault>
	</activation>
	<dependencies>
		<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包(请参照上一篇博客"arquillian-embedded 进行EJB测试"或者官网介绍)

除了配置基础jar包以外,如果要使用自己的jpa实现还需要配置一个单独的jar包(重要)
	<dependency>
		<groupId>org.jboss.shrinkwrap.resolver</groupId>
		<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
		<scope>test</scope>
		<exclusions>
			<exclusion>
				<groupId>com.google.collections</groupId>
				<artifactId>google-collections</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
2、配置arquillian.xml(注意:测试服务器用的为glassfish3.1.1。我使用的测试版本无法在glassfish3.1.2上运行)
<?xml version="1.0"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://jboss.org/schema/arquillian"
	xsi:schemaLocation="http://jboss.org/schema/arquillian
    http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
	<container qualifier="glassfish" default="true">
		<configuration>
			<property name="remoteServerAddress">10.228.XX.XX</property>
			<property name="remoteServerHttpPort">8080</property>
			<property name="remoteServerAdminPort">4848</property>
			<property name="adminUser">admin</property>
			<property name="adminPassword"></property>
		</configuration>
	</container>
</arquillian>

测试代码中需要通过配置的maven resolver的api加入openjpa的依赖
@RunWith(Arquillian.class)
public class TestServiceTest {
	@Deployment
	public static Archive<?> createDeployment() {
		MavenDependencyResolver resolver = DependencyResolvers
				.use(MavenDependencyResolver.class)
				.loadMetadataFromPom("pom.xml");
		WebArchive webInfResource = ShrinkWrap
				.create(WebArchive.class, "arqTest.war")
				.addClass(MyTestService.class)
				.addAsLibraries(resolver.artifact("org.apache.openjpa:openjpa-all:2.1.1").resolveAsFiles()) 
				.addPackages(true, "com.test.model")
				.addAsResource("META-INF/persistence.xml")
				.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
		System.out.println("webInfResource:" + webInfResource);
		return webInfResource;
	}
	@EJB
	private MyTestService rs;
	@Test
	public void testCal() {
		rs.test();
	}
}


你可能感兴趣的:(ejb,测试,application)