我在Wildfly Swarm中使用Arquillian运行EJB测试时遇到问题,它与Hibernate和这个问题有关:wildfly 10: java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory
错误:
ERROR [stderr] (main) Caused by: java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory"},"WFLYCTL0412: Required services that are not installed:" => ["jboss.persistenceunit.\"adaee1b1-6c6b-4f9b-834a-ea36333986b8.jar#AP\""],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:273)
我正在使用Hibernate和JPA,并通过将<scope>provided</scope>
设置为所有Hibernate依赖项来运行Wildfly Swarm时解决了这个问题。但是,当在Arquillian中运行测试时,这不起作用。
ServiceTest.java
@RunWith(Arquillian.class)
public class ServiceTest {
@EJB
private TestService testService;
@Deployment
public static Archive<?> createDeployment() throws Exception {
return new ArchiveBuilder().buildArchive();
}
@Test
public void testService() {
testService.test();
}
}
ArchiveBuilder.java
public class ArchiveBuilder {
public Archive<?> buildArchive() throws Exception {
final JARArchive ejbArchive = ShrinkWrap.create(JARArchive.class);
ejbArchive.addClass(AdapterService.class);
final List<JavaArchive> artifacts = ArtifactLookup.get().allArtifacts(new String[]{"org.wildfly.swarm"});
for (final JavaArchive javaArchive : artifacts) {
ejbArchive.merge(javaArchive);
}
return ejbArchive;
}
}
ArchiveBuilder取自这篇文章:https://dzone.com/articles/testing-ejbs-with-swarm-and-arquillian
pom.xml依赖项
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
<version>2017.5.0</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>ejb</artifactId>
<version>2017.5.0</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jpa</artifactId>
<version>2017.5.0</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
<version>2017.5.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.195</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.2.10.Final</version>
<scope>provided</scope>
</dependency>
如果我修改Hibernate依赖项以删除dom4j.dom4j:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
</exclusions>
</dependency>
我得到了这个:
ERROR [stderr] (main) Caused by: org.wildfly.swarm.container.DeploymentException: WFSWARM0004: Deployment failed: {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"7d32cda0-83d2-47d3-8066-ae1f56899801.jar#AP\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"7d32cda0-83d2-47d3-8066-ae1f56899801.jar#AP\": javax.persistence.PersistenceException: [PersistenceUnit: AP] Unable to build Hibernate SessionFactory
ERROR [stderr] (main) Caused by: javax.persistence.PersistenceException: [PersistenceUnit: AP] Unable to build Hibernate SessionFactory
ERROR [stderr] (main) Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
ERROR [stderr] (main) Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
ERROR [stderr] (main) Caused by: java.lang.reflect.InvocationTargetException
ERROR [stderr] (main) Caused by: java.lang.RuntimeException: by java.lang.NoClassDefFoundError: org/hibernate/proxy/HibernateProxy
ERROR [stderr] (main) Caused by: javassist.CannotCompileException: by java.lang.NoClassDefFoundError: org/hibernate/proxy/HibernateProxy
ERROR [stderr] (main) Caused by: java.lang.NoClassDefFoundError: org/hibernate/proxy/HibernateProxy
ERROR [stderr] (main) Caused by: java.lang.ClassNotFoundException: org.hibernate.proxy.HibernateProxy from [Module \"org.picketbox:main\" from BootModuleLoader@22555ebf for finders [BootstrapClasspathModuleFinder, BootstrapModuleFinder(org.wildfly.swarm.bootstrap:main), ClasspathModuleFinder, ContainerModuleFinder(swarm.container:main), ApplicationModuleFinder(swarm.application:main), org.wildfly.swarm.bootstrap.modules.DynamicModuleFinder@36ebc363]]"},"WFLYCTL0412: Required services that are not installed:" => ["jboss.persistenceunit.\"7d32cda0-83d2-47d3-8066-ae1f56899801.jar#AP\""],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}