title: ClassCastException org.dom4j.DocumentFactory for tests in Wildfly Swarm & Arquillian
body: <p>I have a problem with running EJB tests with Arquillian in Wildfly Swarm, it is related to Hibernate and this question: <a href="https://stackoverflow.com/questions/35533847/wildfly-10-java-lang-classcastexception-org-dom4j-documentfactory-cannot-be-ca">wildfly 10: java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory</a></p>
  
  <p>The error:</p>
  
  <p><code>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:" =&gt; ["jboss.persistenceunit.\"adaee1b1-6c6b-4f9b-834a-ea36333986b8.jar#AP\""],"WFLYCTL0180: Services with missing/unavailable dependencies" =&gt; undefined}
  ERROR [stderr] (main)   at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:273)</code></p>
  
  <p>I am using Hibernate and JPA and solved this when running Wildfly Swarm by setting <code>&lt;scope&gt;provided&lt;/scope&gt;</code> to all Hibernate depedencies. This doesn't work when runnings tests in Arquillian however.</p>
  
  <p><strong>ServiceTest.java</strong></p>
  
  <pre><code>@RunWith(Arquillian.class)
  public class ServiceTest {
      @EJB
      private TestService testService;
  
      @Deployment
      public static Archive&lt;?&gt; createDeployment() throws Exception {
          return new ArchiveBuilder().buildArchive();
      }
  
      @Test
      public void testService() {
          testService.test();
      }
  }
  </code></pre>
  
  <p><strong>ArchiveBuilder.java</strong></p>
  
  <pre><code>public class ArchiveBuilder {
      public Archive&lt;?&gt; buildArchive() throws Exception {
          final JARArchive ejbArchive = ShrinkWrap.create(JARArchive.class);
          ejbArchive.addClass(AdapterService.class);
  
          final List&lt;JavaArchive&gt; artifacts = ArtifactLookup.get().allArtifacts(new String[]{"org.wildfly.swarm"});
  
          for (final JavaArchive javaArchive : artifacts) {
              ejbArchive.merge(javaArchive);
          }
  
          return ejbArchive;
      }
  }
  </code></pre>
  
  <p>ArchiveBuilder is taken from this post: <a href="https://dzone.com/articles/testing-ejbs-with-swarm-and-arquillian" rel="nofollow noreferrer">https://dzone.com/articles/testing-ejbs-with-swarm-and-arquillian</a></p>
  
  <p><strong>pom.xml dependencies</strong></p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;javax&lt;/groupId&gt;
      &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
      &lt;version&gt;7.0&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.wildfly.swarm&lt;/groupId&gt;
      &lt;artifactId&gt;cdi&lt;/artifactId&gt;
      &lt;version&gt;2017.5.0&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.wildfly.swarm&lt;/groupId&gt;
      &lt;artifactId&gt;ejb&lt;/artifactId&gt;
      &lt;version&gt;2017.5.0&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.wildfly.swarm&lt;/groupId&gt;
      &lt;artifactId&gt;jpa&lt;/artifactId&gt;
      &lt;version&gt;2017.5.0&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.wildfly.swarm&lt;/groupId&gt;
      &lt;artifactId&gt;jaxrs&lt;/artifactId&gt;
      &lt;version&gt;2017.5.0&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;com.h2database&lt;/groupId&gt;
      &lt;artifactId&gt;h2&lt;/artifactId&gt;
      &lt;version&gt;1.4.195&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
      &lt;version&gt;5.2.2.Final&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
      &lt;version&gt;5.2.10.Final&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-c3p0&lt;/artifactId&gt;
      &lt;version&gt;5.2.10.Final&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>If I modify the Hibernate dependency to remove dom4j.dom4j:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
      &lt;version&gt;5.2.10.Final&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
      &lt;exclusions&gt;
          &lt;exclusion&gt;
              &lt;groupId&gt;dom4j&lt;/groupId&gt;
              &lt;artifactId&gt;dom4j&lt;/artifactId&gt;
          &lt;/exclusion&gt;
      &lt;/exclusions&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I get this instead:</p>
  
  <p><code>ERROR [stderr] (main) Caused by: org.wildfly.swarm.container.DeploymentException: WFSWARM0004: Deployment failed: {"WFLYCTL0080: Failed services" =&gt; {"jboss.persistenceunit.\"7d32cda0-83d2-47d3-8066-ae1f56899801.jar#AP\"" =&gt; "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:" =&gt; ["jboss.persistenceunit.\"7d32cda0-83d2-47d3-8066-ae1f56899801.jar#AP\""],"WFLYCTL0180: Services with missing/unavailable dependencies" =&gt; undefined}</code></p>