当前位置: 代码迷 >> 嵌入开发 >> arquillian-embedded 开展EJB测试
  详细解决方案

arquillian-embedded 开展EJB测试

热度:543   发布时间:2016-04-25 08:43:54.0
arquillian-embedded 进行EJB测试

arquillian测试框架具有普通测试框架无法做到的是功能,对容器依赖的EJB测试,当然这个测试过程是要比普通的测试要稍微复杂一些

一、基于测试框架内置容器和内置数据库(derby)的测试

此配置在官网有详细的示例,此处就不再重复,请参考http://arquillian.org/guides/

二、配置自己的数据库为测试数据库(使用db2)

1、配置pom.xml (使用openjpa作为项目的jpa实现)
由于此测试框架有自己的一套JavaEE的实现jar包,所以会造成与工程内部的jsf和javaee6 api等冲突,使用测试框架时不能启用此jar包(通过配置profile,可参考arquillian官网示例介绍)。
	<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>

配置glassfish-embedded (放到单独的profile中),db2的jar包不包含在maven库中,因此需要在本地进行配置,也可以不在pom中加入,在build path中添加
	<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>

2、配置arquillian.xml (此配置文件为测试框架默认读取的配置文件)
配置glassfish的资源文件的路径
<?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();	}}





  相关解决方案