GlassFish 3ã§ArquillianãåãããArquillianã使ç¨ããJUnitã§ãCDIã§EJBå ¥ãã¦JPAã§ã¢ã¯ã»ã¹ããã®ãåãã¨ããã¾ã§ãããArquillianã®åä½ã¢ã¼ãã¯ãembeded,remote,manageã®ä¸ç¨®é¡ããããembededããæ軽ãªãã ãã©ããã®ã¨ã³ããªã§ã¯remoteã§ããã
ç°å¢
ãã£ããã¨
ã¾ãã¯Eclipseã®ããã¸ã§ã¯ãæ§æã
> tree /F C:. â .classpath â .project â pom.xml â ââsrc â ââmain â â ââjava â â â ââkagamihoge â â â ââgf3arq â â â HogeEJB.java â â â HogePOJO.java â â â â â ââresources â â ââwebapp â â â index.jsp â â â â â ââWEB-INF â â beans.xml â â â ââtest â ââjava â â ââkagamihoge â â ââgf3arq â â HogeTest.java â â Resources.java â â â ââresources â ââMETA-INF â test-persistence.xml â
pom.xmlã®ä¾åæ§ã«ããããé¨åã ããæç²ã
<dependencyManagement> <!-- Arquillian API --> <dependencies> <dependency> <groupId>org.jboss.arquillian</groupId> <artifactId>arquillian-bom</artifactId> <version>1.1.1.Final</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Arquillian-JUnitã¤ã³ãã°ã¬ã¼ã·ã§ã³ --> <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <scope>test</scope> </dependency> <!-- Embedded GlassFishã³ã³ããã¢ããã¿ï¼remoteç¨ï¼ --> <dependency> <groupId>org.jboss.arquillian.container</groupId> <artifactId>arquillian-glassfish-remote-3.1</artifactId> <version>1.0.0.CR4</version> <scope>test</scope> </dependency> <!-- å¾è¿° --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.11</version> </dependency> <!-- å¾è¿° --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version> </dependency> <!-- å¾è¿°ãJavaEE6ã®ä¾åæ§ããã®ä½ç½®ã«ãããã¨ãéè¦ --> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies>
Arquillianã使ç¨ããJUnitã®ã³ã¼ãã
package kagamihoge.gf3arq; import java.nio.file.Path; import java.nio.file.Paths; import javax.inject.Inject; import kagamihoge.gf3arq.HogeEJB; import kagamihoge.gf3arq.HogePOJO; 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.spec.WebArchive; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(Arquillian.class) public class HogeTest { @Deployment public static WebArchive createDeployment() { Path webinf = Paths.get( "src", "main", "webapp", "WEB-INF"); Path testPersistenceXML = Paths.get( "src", "test", "resources", "META-INF", "test-persistence.xml"); WebArchive arch = ShrinkWrap.create(WebArchive.class, "test-hoge.war") .addClasses(HogeEJB.class, HogePOJO.class, Resources.class) .addAsWebInfResource(webinf.resolve("beans.xml").toFile()) .addAsResource(testPersistenceXML.toFile(), "META-INF/persistence.xml"); //âã®ããã«ãããã¨ã§ãArquillianã«æ¸¡ãããã£ã¬ã¯ããªæ§æãåç §ã§ããã System.out.println(arch.toString(true)); return arch; } @Inject private HogeEJB hogeEJB; @Test public void testInsert() { hogeEJB.insert(); } @Test public void testSelect() { hogeEJB.select(); } }
ããã§ã¯ãbeans.xmlã¯ãããã¯ã·ã§ã³ã¨åçã®ãã®ãpersistence.xmlã¯ãã¹ãç¨ã®ä»»æã®ãã¡ã¤ã«ã使ããã¨ããæ³å®ã§æ¸ãã¦ãããå ·ä½çã«ã¯ãsrc/test/resources/META-INF/test-persistence.xml ã META-INF/persistence.xml ã¨ãã¦æ±ãã³ã¼ãã«ãã¦ããããã¹ãç¨ã¨æ¬çªç¨ã§å¥ã®XMLã使ãç´ æ´ãªä¾ã ããããå°ãé«åº¦ãªããæ¹ã¯Testing Java Persistence · Arquillian Guidesãåç §ã
arch.toString(true)ã®é¨åã¯ä¸è¨ã®ãããªåºåã«ãªãããããã°ã¨ãã¦ä½¿ç¨ãããbeans.xmlãpersistence.xmlãæå®ã®ãã£ã¬ã¯ããªã«åã¾ã£ã¦ããããã¨ãã®ç¢ºèªã«ä½¿ããã
test-hoge.war: /WEB-INF/ /WEB-INF/beans.xml /WEB-INF/classes/ /WEB-INF/classes/META-INF/ /WEB-INF/classes/META-INF/persistence.xml /WEB-INF/classes/kagamihoge/ /WEB-INF/classes/kagamihoge/gf3arq/ /WEB-INF/classes/kagamihoge/gf3arq/Resources.class /WEB-INF/classes/kagamihoge/gf3arq/HogeEJB.class /WEB-INF/classes/kagamihoge/gf3arq/HogePOJO.class
beans.xml ããã¯ä¾ã«ãã£ã¦ãã¼ã«ã¼ç¨ã®ã«ã©ã®ãã¡ã¤ã«ã
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> </beans>
EJBã®ã³ã¼ããInjectããJUnit -> EJB -> POJOã試ãããã£ãã®ã§ãEJBã¯ç¹ã«ä½ãããªãã³ã¼ãã«ãªã£ã¦ããã
package kagamihoge.gf3arq; import javax.ejb.LocalBean; import javax.ejb.Stateless; import javax.inject.Inject; @Stateless @LocalBean public class HogeEJB { @Inject private HogePOJO hogePOJO; public HogeEJB() { } public void select() { hogePOJO.select(); } public void insert() { hogePOJO.insert(); } }
EJBããåç §ãããPOJOã®ã³ã¼ããEntityManagerãinjectãã¦ãã¼ã¿ã¢ã¯ã»ã¹ããã
package kagamihoge.gf3arq; import java.util.List; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.Query; public class HogePOJO { @Inject private EntityManager em; public HogePOJO() { super(); } public void insert() { Query q = em.createNativeQuery( "insert into users(id, name, email) values('14', 'ddd', '[email protected]')"); System.out.println("###insert count:" + q.executeUpdate()); } public void select() { Query q = em.createNativeQuery("select table_name from user_tables"); List r = q.getResultList(); System.out.println("###table names"); for (Object tableName : r) { System.out.println("##"+ tableName); } } }
EntityManagerã®ã¤ã³ã¹ã¿ã³ã¹ãProducesããããã®ã¯ã©ã¹ã詳細ã¯å¾è¿°ã
package kagamihoge.gf3arq; import javax.enterprise.inject.Disposes; import javax.enterprise.inject.Produces; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; public class Resources { @PersistenceUnit(unitName="testdb") private EntityManagerFactory emf; @Produces public EntityManager getEntityManager() { System.out.println("#####open em"); return emf.createEntityManager(); } public void closeEntityManager(@Disposes EntityManager em) { System.out.println("#####close em"); em.close(); } }
src/test/resources/META-INF/test-persistence.xml ãã¾ãæ·±ãæå³ã¯ç¡ããã ãunitãäºã¤å®ç¾©ãã¦ããããã¼ã¿ã½ã¼ã¹ã«é¢ãã¦ã¯GlassFish4でOracle11gXEのJDBC接続をつくる - kagamihogeの日記çã§äºåã«ä½ã£ã¦ãããã®ã¨ããã
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="testdb" transaction-type="JTA"> <jta-data-source>jdbc/Oracle11gXE</jta-data-source> </persistence-unit> <persistence-unit name="hoge" transaction-type="JTA"> <jta-data-source>jdbc/Oracle11gXE</jta-data-source> </persistence-unit> </persistence>
å®è¡ããã¨ãã®glassfishã®ãµã¼ããã°ã/test-hogeã§ãããã¤ãemçæããã¹ãå®è¡ã§insertã¨selectãemç ´æ£ãã¢ã³ãããã¤ãã¨æµãã¦ããã®ãè¦ã¦åããã
æ å ±: EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461 æ å ±: file:/C:/Java/glassfish/glassfish-3.1.2.2/glassfish/domains/domain1/applications/test-hoge/WEB-INF/classes/_testdb login successful è¦å: Multiple [2] JMX MBeanServer instances exist, we will use the server at index [0] : [com.sun.enterprise.v3.admin.DynamicInterceptor@18551ae]. è¦å: JMX MBeanServer in use: [com.sun.enterprise.v3.admin.DynamicInterceptor@18551ae] from index [0] è¦å: JMX MBeanServer in use: [com.sun.jmx.mbeanserver.JmxMBeanServer@3e7bf0] from index [1] è¦å: The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element æ å ±: EJB5181:Portable JNDI names for EJB HogeEJB: [java:global/test-hoge/HogeEJB!kagamihoge.gf3arq.HogeEJB, java:global/test-hoge/HogeEJB] æ å ±: WEB0671: Loading application [test-hoge] at [/test-hoge] æ å ±: test-hogeã¯ã1,328ããªç§ã§æ£å¸¸ã«ãããã¤ããã¾ããã æ å ±: Deleting file.... æ å ±: #####open em æ å ±: ###insert count:1 æ å ±: ###table names æ å ±: ##USERS æ å ±: ##HOGE æ å ±: ##CUSTOMER2 æ å ±: ##ORDERS2 æ å ±: ##CUSTOMER3 æ å ±: ##ORDERS3 æ å ±: ##N_DEPT æ å ±: ##N_EMP æ å ±: ##EMP æ å ±: ##DEPT æ å ±: ##C_EMP é大: No valid EE environment for injection of kagamihoge.gf3arq.Resources æ å ±: #####close em æ å ±: file:/C:/Java/glassfish/glassfish-3.1.2.2/glassfish/domains/domain1/applications/test-hoge/WEB-INF/classes/_testdb logout successful
ã¯ã¾ãã©ãã
ããã£ãããæ©ãã ãããã¨ããã
Missing artifact org.jboss.arquillian:arquillian-bom:jar:1.1.1.Final
Missing artifact org.jboss.arquillian:arquillian-bom:jar:1.1.1.Final
bomãªã®ã§dependenciesã»ã¯ã·ã§ã³ã§ã¯ãªããdependencyManagementã»ã¯ã·ã§ã³ã«å ¥ããå¿ è¦ããããã¾ããrepositoryã«JBossãå ¥ããå¿ è¦ãããã
<repository> <id>JBoss Repo</id> <url>http://repository.jboss.org/nexus/content/groups/public-jboss/</url> <name>JBoss Repo</name> </repository>
åèï¼
Absent Code attribute in method that is not native or abstract in class file
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ws/rs/core/MediaType
ãã®åå ãããç°¡åã«è¨ãã°ãjavax/ws/rs/core/MediaTypeã®å®ä½ãç¡ãããã«èµ·ãããããã®è§£æ¶ã¯ã¾ããä¸è¨ã®ããã«ä¾åæ§ã追å ãã¦ããããªããglassfish 3.1.2.2ã®å ´åãªã®ã§ãä»ãã¼ã¸ã§ã³ãä»ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã®å ´åã¯ç°ãªãå¯è½æ§ãããã¨æããããã¾ããjavaeeã®ä¾åæ§ï¼ããã§ã¯javaee-web-apiï¼ãä¸çªæå¾ã«æã£ã¦ããã
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.11</version> </dependency> .... <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency>
ã©ããã¦ããããªãã°ãªããªãã®ãã¯ä¿ºèªèº«è©³ãããã¨ã¯è¯ãç解ãã¦ããªãã®ã ãã©ãã¾ããjavaee-web-apiã¯ã¤ã³ã¿ãã§ã¼ã¹ãªã©ã®ã¿ã§å®è£ ã¯ä¿æãã¦ããªããããã¯æ¨ä»ã®j2eeã§ã¯æ®éã®ãã¨ã§ãããããarquillianã¯ãã®éä¸ã§å®è£ ãè¦æ±ï¼ããã®ã ããã¤ã³ã¿ãã§ã¼ã¹ããç¡ãã®ã§ãã®ä¾å¤ãçºçããï¼ãããï¼ããªã®ã§ãã¾ãã¯è¶³ããªãå®è£ ï¼ããã§ã¯jersey-serverï¼ã追å ããæ´ã«å®è£ ãå ã«èªãã§ã¤ã³ã¿ãã§ã¼ã¹ãå¾åãã«ããããã«javaee-web-apiãå¾ã«æã£ã¦ããâ¦â¦ãã¨ã§ä¸æãè¡ããããã
ä»ã®è§£æ±ºçã¨ãã¦ãã¯ã©ã¹ãã¹ã«ãã¼ã«ã«ã«ã¤ã³ã¹ãã¼ã«ããã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã®ã©ã¤ãã©ãªã追å ããã¨ããjboss-specã¨ãã®ä¾åæ§ã§å®è£ ãã¾ãã£ã¨å ¥ããã¨ãããä¸ãããã¦ããã
ã¾ãããã®ç°å¢ã ã¨ä¸è¨ã®ä¾å¤ãçºçããã
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/internet/ParseException
ãã£ã¦ãä¸è¨ã®ä¾åæ§ã追å ãã¦å¯¾å¦ã
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version> </dependency>
åèï¼
- http://stackoverflow.com/questions/3424207/testing-against-java-ee-6-api
- What's the cause of this exception: java.lang.C... | Community åå ã®è§£èª¬ãããã¦ãã
JUnitã§injectããejbã®ã¤ã³ã¹ã¿ã³ã¹ãNullPointerException
beans.xmlããªããglassfishï¼weldï¼ï¼ã®ãã¼ã¸ã§ã³ã«ãã£ã¦ã¯injectã§ãªãejbã¢ããã¼ã·ã§ã³ããªã©ãbeans.xmlãå ¥ãã¦ããã¤ããã§ãã/WEB-INF/beans.xmlã¨ãã®ãã£ã¬ã¯ããªã«å ¥ã£ã¦ãªããã¨ããããWebArchive#toString(true)ã§ãããã°åºåããã¦ç¢ºèªããã
Could not connect to DAS
org.jboss.arquillian.container.spi.client.container.LifecycleException: Could not connect to DAS on: http://localhost:4848 | Connection refused: connect
remoteã®å ´åãJUnitå®è¡åã«GlassFishãµã¼ããèµ·åãã¦ããªããã°ãªããªããèµ·åããã«JUnitå®è¡ããã¨ãããªä¾å¤ãåºãã
WELD-001408 Unsatisfied dependencies for type [EntityManager]
com.sun.jersey.api.container.ContainerException: exit_code: FAILURE, message: ãããã¤ã¡ã³ãä¸ã«ã¨ã©ã¼ãçºçãã¾ãã: Exception while loading the app : WELD-001408 Unsatisfied dependencies for type [EntityManager] with qualifiers [@Default] at injection point [[field] @Inject private kagamihoge.gf3arq.HogePOJO.em]ã詳細ã¯server.logãåç §ãã¦ãã ããã [status: CLIENT_ERROR reason: Bad Request]
@Inject private EntityManager em;
EntityManagerã¯Injectã¢ããã¼ã·ã§ã³ã§ã¯ãªããPersistenceContextã¢ããã¼ã·ã§ã³ã使ç¨ããã
@PersistenceContext(unitName="testdb") private EntityManager em;
ããã®åå ã¯ãCDIãEntityManagerã®ã¤ã³ã¹ã¿ã³ã¹ã®çææ¹æ³ãç¥ããªãããçºçããããªã®ã§ãPersistenceContextã¨å°ç¨ã®ã¢ããã¼ã·ã§ã³ã使ã£ã¦ããã°è¯ãã
ããããããã¯è¨ã£ã¦ãInjectã§ãããã¦ããããã¨ããã®ã人æ ã§ãããCDIãEntityManagerã®ã¤ã³ã¹ã¿ã³ã¹ã®ä½ææ¹æ³ãç¥ã£ã¦ãããããã°ããã®ã§ãä¸è¨ã®ãããªã¯ã©ã¹ãä½æãããä¸è¨ã®ã³ã¼ãã¯ãPersistenceContextã§ã¤ã³ã¹ã¿ã³ã¹ãæ³¨å ¥ããProducesã¢ããã¼ã·ã§ã³ã§EntityManagerã®ã¤ã³ã¹ã¿ã³ã¹ã¯ã³ã¬ã使ããã¨æ示ãã¦ããã
public class Resources { @SuppressWarnings("unused") @Produces @PersistenceContext(unitName="testdb") private EntityManager em;
ä¸å¿ããã§ãåé¡ãªãã®ã ããã³ã³ãã管çã¨ããã¦ãããã§ã¯ãªãã®ã§ã誰ãcloseãã¦ããªããã©ãããããã¤ï¼ã¢ã³ãããã¤ãããã ããç¥ã£ããã¨ããã¨ããã®ãä¸æã§ã¯ããã ãããããã¯ãcloseãã¦ããããã
ä¸è¿°ã®Resourcesãæ¸ãå ãããDisposesã¢ããã¼ã·ã§ã³ãä»ä¸ããã¡ã½ãããä½ããããã§closeããããããã®å®è¡çµæã¯ãã®ã¨ã³ããªã®ä¸ã®æ¹ãåç §ã
public class Resources { @PersistenceUnit(unitName="testdb") private EntityManagerFactory emf; @Produces public EntityManager getEntityManager() { System.out.println("#####open em"); return emf.createEntityManager(); } public void closeEntityManager(@Disposes EntityManager em) { System.out.println("#####close em"); em.close(); } }
ãªããä¸è¨ã®ãããªæ¸ãæ¹ã¯ãã¡ã§ããã
public class Resources { @SuppressWarnings("unused") @Produces @PersistenceContext(unitName="testdb") private EntityManager em; public void closeEntityManager(@Disposes EntityManager em) { System.out.println("#####close em"); em.close(); } }
ãããªæãã®ä¾å¤ãçºçããããªã®ã§ãEntityManagerãåå¾ããã¨ããã¯ããããã£ããã¡ã½ããã«å¤æ´ãã¦ããã
com.sun.jersey.api.container.ContainerException: exit_code: FAILURE, message: ãããã¤ã¡ã³ãä¸ã«ã¨ã©ã¼ãçºçãã¾ãã: Exception while loading the app : WELD-001424 The following disposal methods were declared but did not resolve to a producer method: [Disposer method [[method] public gf3arq.Resources.closeEntityManager(EntityManager)]]ã詳細ã¯server.logãåç §ãã¦ãã ããã [status: CLIENT_ERROR reason: Bad Request]
åèï¼
No valid EE environment for injection
é大: No valid EE environment for injection of gf3arq.Resources
è¬ãåé¡ç¡ãåããã§ç¡è¦ããã
ææ³ã¨ã
ã¶ã£ã¡ããããã¾ã§ä½ãã®ã«ãæ°æ¥ãè¦ãã¦ããããã³ãStack Overflowæ§ã ãã§ã
Arquillian, CDI(Weld), EJB, JPA, Mavenãå ¥ãä¹±ããã®ã§ããããã®ç¥èã¯å½ç¶ã®ããã«è¦æ±ããããGlassFishãªãJBossãªãã®ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã®ç¥èãè¦ãããã®ä¸ã§æ´ã«ãJUnitã§å¹æçãªãã¹ãã±ã¼ã¹ãçµããã¹ãã«ããã£ã¦ã¯ããã¦ãArquillianãæ´»ãã¦æ¥ãããªã®ã§ãArquillian使ãæ·å± ã£ã¦çµæ§é«ããã§ãªããããâ¦â¦ãªã©ã¨æãã®ã§ãã£ã¦ãããã¾ããJ2EEã£ã¦ãã¼ããããã¨ããã°ãã¼ãããããªãã ãã ãã©ããã