2015年11月20日 | Leave a comment 完整示例下载: UT2 EJBContainer 不能加载glass-resources.xml , Mock 也可以,不过需要调整一些代码,把 EntityManager 这种东西set好 所以选择了Arquillian Arquillian +embedded glassfish pom.xml见后 关键点: pom.xml + dependencyManagement arquillian-bom pom.xml + arquillian-junit-container pom.xml + arquillian-glassfish-embedded-3.1 pom.xml + glassfish-embedded-all <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>me.yek</groupId> <artifactId>UT2</artifactId> <version>1.0</version> <packaging>war</packaging> <name>UT2</name> <properties> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId> <version>2.5.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <version>1.1.10.Final</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.container</groupId> <artifactId>arquillian-glassfish-embedded-3.1</artifactId> <version>1.0.0.CR4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.main.extras</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>4.1.1</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!-- begin --> <!-- end --> <!-- bbb--> <!-- Alternative to Hamcrest matchers. Provides fluent, type-aware API --> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.arquillian</groupId> <artifactId>arquillian-bom</artifactId> <version>1.1.10.Final</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <compilerArguments> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <phase>validate</phase> <goals> <goal>copy</goal> </goals> <configuration> <outputDirectory>${endorsed.dir}</outputDirectory> <silent>true</silent> <artifactItems> <artifactItem> <groupId>javax</groupId> <artifactId>javaee-endorsed-api</artifactId> <version>7.0</version> <type>jar</type> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>me.yek</groupId> <artifactId>UT2</artifactId> <version>1.0</version> <packaging>war</packaging> <name>UT2</name> <properties> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId> <version>2.5.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <version>1.1.10.Final</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.container</groupId> <artifactId>arquillian-glassfish-embedded-3.1</artifactId> <version>1.0.0.CR4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.main.extras</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>4.1.1</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!-- begin --> <!-- end --> <!-- bbb--> <!-- Alternative to Hamcrest matchers. Provides fluent, type-aware API --> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.arquillian</groupId> <artifactId>arquillian-bom</artifactId> <version>1.1.10.Final</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <compilerArguments> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <phase>validate</phase> <goals> <goal>copy</goal> </goals> <configuration> <outputDirectory>${endorsed.dir}</outputDirectory> <silent>true</silent> <artifactItems> <artifactItem> <groupId>javax</groupId> <artifactId>javaee-endorsed-api</artifactId> <version>7.0</version> <type>jar</type> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> unitest: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package me.yek.ut2; import java.io.File; import java.util.List; import javax.ejb.EJB; 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.asset.FileAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Test; import static org.junit.Assert.*; import org.junit.runner.RunWith; /** * * @author sihai */ @RunWith(Arquillian.class) public class MyBeanTest { //not work @Deployment // testGetAll(me.yek.ut2.MyBeanTest): ArquillianServletRunner not found. Could not determine ContextRoot from ProtocolMetadata, please contact DeployableContainer developer. public static WebArchive _createDeployment() { //"/Users/sihai/Desktop/JavaEE/UT2/src/main/webapp/WEB-INF/glassfish-resources.xml" WebArchive ret= ShrinkWrap.create(WebArchive.class) .addClasses(MyBean.class) //.addAsManifestResource(new File("src/main/resources/META-INF/persistence.xml"), "persistence.xml") .addAsResource(new File("src/main/resources/META-INF/persistence.xml"), "META-INF/persistence.xml") .add(new FileAsset(new File("src/main/webapp/WEB-INF/glassfish-resources.xml")),"WEB-INF/glassfish-resources.xml"); System.out.println(ret.toString(true)); return ret; } @Deployment public static WebArchive createDeployment() { //"/Users/sihai/Desktop/JavaEE/UT2/src/main/webapp/WEB-INF/glassfish-resources.xml" WebArchive ret= ShrinkWrap.create(WebArchive.class) .addClass(MyBean.class) //.addAsManifestResource(new File("src/main/resources/META-INF/persistence.xml"), "persistence.xml") .addAsResource(new File("src/main/resources/META-INF/persistence.xml"), "META-INF/persistence.xml") .add(new FileAsset(new File("src/main/webapp/WEB-INF/glassfish-resources.xml")),"WEB-INF/glassfish-resources.xml"); System.out.println(ret.toString(true)); return ret; } @EJB MyBean instance; @Test public void testGetAll() { System.out.println("getAll"); List expResult = null; List result = instance.getAll(); assertTrue(result.size()>0); } } 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package me.yek.ut2; import java.io.File;import java.util.List;import javax.ejb.EJB;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.asset.FileAsset;import org.jboss.shrinkwrap.api.spec.JavaArchive;import org.jboss.shrinkwrap.api.spec.WebArchive;import org.junit.Test;import static org.junit.Assert.*;import org.junit.runner.RunWith; /** * * @author sihai */@RunWith(Arquillian.class)public class MyBeanTest { //not work @Deployment // testGetAll(me.yek.ut2.MyBeanTest): ArquillianServletRunner not found. Could not determine ContextRoot from ProtocolMetadata, please contact DeployableContainer developer. public static WebArchive _createDeployment() { //"/Users/sihai/Desktop/JavaEE/UT2/src/main/webapp/WEB-INF/glassfish-resources.xml" WebArchive ret= ShrinkWrap.create(WebArchive.class) .addClasses(MyBean.class) //.addAsManifestResource(new File("src/main/resources/META-INF/persistence.xml"), "persistence.xml") .addAsResource(new File("src/main/resources/META-INF/persistence.xml"), "META-INF/persistence.xml") .add(new FileAsset(new File("src/main/webapp/WEB-INF/glassfish-resources.xml")),"WEB-INF/glassfish-resources.xml"); System.out.println(ret.toString(true)); return ret; } @Deployment public static WebArchive createDeployment() { //"/Users/sihai/Desktop/JavaEE/UT2/src/main/webapp/WEB-INF/glassfish-resources.xml" WebArchive ret= ShrinkWrap.create(WebArchive.class) .addClass(MyBean.class) //.addAsManifestResource(new File("src/main/resources/META-INF/persistence.xml"), "persistence.xml") .addAsResource(new File("src/main/resources/META-INF/persistence.xml"), "META-INF/persistence.xml") .add(new FileAsset(new File("src/main/webapp/WEB-INF/glassfish-resources.xml")),"WEB-INF/glassfish-resources.xml"); System.out.println(ret.toString(true)); return ret; } @EJB MyBean instance; @Test public void testGetAll() { System.out.println("getAll"); List expResult = null; List result = instance.getAll(); assertTrue(result.size()>0); } } 完整示例下载: UT2