2 Replies Latest reply on Sep 16, 2013 12:12 AM by M Mudu

    ClassNotFoundException when running EJB test

    M Mudu Newbie

      The directory structure for running a JUnit test using Arquillian is:

       

      my-app-ejb

           src/

                main/

                        java/

                         my-app-ejb/

                                        ejb/  

                                              account/

                                                   AccountBean.java                      

                                                   AcoountLocal.java

           src/main/resources

           src/

                test/

                     java/  

                          my-app-ejb/

                                        ejb/  

                                              account/

                                                   AccountUnitTest.java

           src/test/resources

                arquillian.xml

          /Maven Dependencies

          /JRE System Library [JavaSE-1.7]

           /build

           /resources

           /target

           pom.xml

       

      Here is the content of my pom.xml:

       

      <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>my-app-ejb</groupId>

        <artifactId>my-app-ejb</artifactId>

        <version>0.0.1-SNAPSHOT</version>

        <packaging>jar</packaging>

       

       

        <properties>

             <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        </properties>

       

       

        <build>

             <plugins>

                  <plugin>

                       <artifactId>maven-compiler-plugin</artifactId>

                       <version>3.1</version>

                       <configuration>

                            <source>1.6</source>

                            <target>1.6</target>

                      </configuration>

                  </plugin>

             <plugin>

                  <artifactId>maven-surefire-plugin</artifactId>

                  <version>2.15</version>

             </plugin>

        </plugins>

        </build>

      <dependencyManagement>

             <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>

             <dependency>

             <groupId>junit</groupId>

            <artifactId>junit</artifactId>

             <version>4.11</version>

             <scope>test</scope>

        </dependency>

      .

      .

      .

         <dependency>

             <groupId>org.jboss.arquillian</groupId>

             <artifactId>arquillian-bom</artifactId>

             <version>1.1.1.Final</version>

             <scope>import</scope>

             <type>pom</type>

         </dependency>

        <dependency>

             <groupId>org.jboss.arquillian.junit</groupId>

             <artifactId>arquillian-junit-container</artifactId>

             <scope>test</scope>

        </dependency>

        <dependency>

             <groupId>xalan</groupId>

             <artifactId>xalan</artifactId>

             <version>2.7.1</version>

        </dependency>

        </dependencies>

      <profiles>

        <profile>

             <id>arquillian-jbossas-managed</id>

             <dependencies>

                    <dependency>

                       <groupId>org.jboss.bom</groupId>

                       <artifactId>jboss-javaee-6.0-with-all</artifactId>

                       <version>1.0.7.CR10</version>

                       <type>pom</type>

                  </dependency>

                  <dependency>

                       <groupId>org.jboss.as</groupId>

                       <artifactId>jboss-as-arquillian-container-managed</artifactId>

                       <version>7.2.0.Final</version>

                       <scope>test</scope> 

                  </dependency>

                  <dependency>

                       <groupId>org.jboss.arquillian.protocol</groupId>

                       <artifactId>arquillian-protocol-servlet</artifactId>

                       <scope>test</scope>

                  </dependency>

             </dependencies>

        </profile>

        </profiles>

       

      I am only trying to run 1 test (see below)

       

      @RunWith(Arquillian.class)

      public class AccountUnitTest {

       

       

        @Deployment

          public static JavaArchive createDeployment() {

              return ShrinkWrap.create(JavaArchive.class)

                  .addClass(AccountBean.class)

                  .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

          }

       

        @EJB

        AccountLocal accountLocal;

       

      @Test

        public void findUser(){

             UserValue user = accountLocal.findUser("admin");

             boolean userFound = false;

             if(user != null) {

                  userFound = true;

             }

        Assert.assertTrue(userFound);

        }

       

      I get the ClassNotFoundException for the AccountLocal interface:

       

      Running com.my-app-ejb.test.account.AccountUnitTest

      Aug 30, 2013 3:14:59 PM org.jboss.as.arquillian.container.managed.ManagedDeployableContainer startInternal

      INFO: Starting container with: [/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java, -Xmx512m, -XX:MaxPermSize=128m, -ea, -Djboss.home.dir=/opt/jboss7/jboss-eap-6.1, -Dorg.jboss.boot.log.file=/opt/jboss7/jboss-eap-6.1/standalone/log/boot.log, -Dlogging.configuration=file:/opt/jboss7/jboss-eap-6.1/standalone/configuration/logging.properties, -Djboss.bundles.dir=/opt/jboss7/jboss-eap-6.1/bundles, -jar, /opt/jboss7/jboss-eap-6.1/jboss-modules.jar, -mp, /opt/jboss7/jboss-eap-6.1/modules, -jaxpmodule, javax.xml.jaxp-provider, org.jboss.as.standalone, -server-config, standalone.xml]

      15:15:00,509 INFO  [org.jboss.as.server] (Controller Boot Thread) JBAS015888: Creating http management service using socket-binding (management-http)

      15:15:01,009 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-9) JBAS015012: Started FileSystemDeploymentService for directory /opt/jboss7/jboss-eap-6.1/standalone/deployments

      15:15:06,373 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015876: Starting deployment of "test.war" (runtime-name: "test.war")

      15:15:06,676 WARN  [org.jboss.modules] (MSC service thread 1-13) Failed to define class com.my-app-ejb.ejb.account.AccountBean in Module "deployment.test.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link com/my-app-ejb/ejb/account/AccountBean (Module "deployment.test.war:main" from Service Module Loader)

        at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:427) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:260) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:75) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.Module.loadModuleClass(Module.java:526) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:188) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:444) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:432) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:374) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:119) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at java.lang.Class.forName0(Native Method) [rt.jar:1.7.0_25]

        at java.lang.Class.forName(Class.java:270) [rt.jar:1.7.0_25]

        at org.jboss.as.server.deployment.reflect.DeploymentClassIndex.classIndex(DeploymentClassIndex.java:54)

        at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.processComponentConfig(InterceptorAnnotationProcessor.java:85) [jboss-as-ee-7.2.0.Final-redhat-8.jar:7.2.0.Final-redhat-8]

        at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.deploy(InterceptorAnnotationProcessor.java:77) [jboss-as-ee-7.2.0.Final-redhat-8.jar:7.2.0.Final-redhat-8]

        at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:120)

        at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)

        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)

        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_25]

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_25]

        at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]

      Caused by: java.lang.NoClassDefFoundError: com/my-app-ejb/ejb/account/AccountLocal

        at java.lang.ClassLoader.defineClass1(Native Method) [rt.jar:1.7.0_25]

        at java.lang.ClassLoader.defineClass(ClassLoader.java:788) [rt.jar:1.7.0_25]

        at org.jboss.modules.ModuleClassLoader.doDefineOrLoadClass(ModuleClassLoader.java:344) [jboss-modules.jar:1.2.0.Final-redhat-1]

        at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:422) [jboss-modules.jar:1.2.0.Final-redhat-1]

        ... 19 more

      Caused by: java.lang.ClassNotFoundException: com.my-app-ejb.ejb.account.AccountLocal from [Module "deployment.test.war:main" from Service Module Loader]

        at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:196) [jboss-modules.jar:1.2.0.Final-redhat-1]

       

      When running the test from Eclipse I can see that the AccountLocal.java interface is missing from the deployed .jar (see below).

       

      09:20:13,271 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-11) JBAS015012: Started FileSystemDeploymentService for directory /opt/jboss7/jboss-eap-6.1/standalone/deployments

      97c43801-ca88-4784-94ef-13a055e936c4.jar:

      /com/

      /com/my-app-ejb/

      /com/my-app-ejb/

      /com/my-app-ejb/ejb/

      /com/my-app-ejb/ejb/account/

      /com/my-app-ejb/ejb/account/AccountBean.class

      /META-INF/

      /META-INF/beans.xml

       

      Why is the AccountLocal.class not included in the .jar?

      Has anyone any idea what I am doing wrong?

       

      I have found it.  I have to add the AccountLocal.class to the ShrinkWrap contructor when creating the jar like:

       

      JavaArchive jar = ShrinkWrap.create(JavaArchive.class)

             .addClass(AccountLocal.class)

             .addClass(AccountBean.class)

             .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

             System.out.println(jar.toString(true));

             return jar;

       

      Now I have just to add all the .class files in a package to the .jar and then it should work.