5 Replies Latest reply on Oct 31, 2010 9:47 PM by markwen97

    JPA TestCase Triggered ClassNotFoundException: org.jboss.weld.resources.spi.ResourceLoadingException

    markwen97

      Hi,

       

      I was trying to test my JPA DAO classes, and I referred to the following resources:

      1. http://community.jboss.org/en/arquillian/blog/2010/10/04/the-perfect-recipe-for-testing-jpa-2-revisited

      2. http://docs.jboss.org/arquillian/reference

       

      My JPA test case is more complicated than the examples. Since my JPA DAO classes are not configured as EJB, I cannot use @EJB annotation in the test class, so @Inject is used. Below is some code snippets:

       

      @RunWith(Arquillian.class)
      public class GenericDaoTestCase

      {

       

         @Deployment
         public static Archive<?> createDeployment()
         {
            return ShrinkWrap.create(WebArchive.class, "test.war")
                  .addClasses(Role.class)
                  .addClasses(GenericDao.class)
                  .addClasses(GenericDaoHibernate.class)
                  .addManifestResource("test-persistence.xml", "persistence.xml")
                  .addWebResource(EmptyAsset.INSTANCE, "beans.xml");
          }

         private static final Object log = System.out;
        
         @Inject
         UserTransaction utx;
        
         @Inject
         private GenericDaoHibernate genericDao;
         
          @Test
          public void testSave() throws Exception {
         
              utx.begin();

       

              Role role = new Role();
              role.setName("Test Role");
             
              role = genericDao.save(role);
             
              assertNotNull(role.getId());
             
              utx.commit();
          }

       

      }

       

      When I run the test case with "mvn clean test -Parq-jbossas-managed" in the command line, the test case was deployed to JBoss AS 6 sucessfully, but surefire reported the following exception:

       

      org.jboss.arquillian.impl.event.FiredEventException: java.lang.IllegalStateException: Error launching test com.app.domain.GenericDaoTestCase public void com.app.domain.GenericDaoTestCase.testSave() throws java.lang.Exception
          at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:68)
          at org.jboss.arquillian.impl.context.AbstractEventContext.fire(AbstractEventContext.java:115)
          at org.jboss.arquillian.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:157)
          at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:244)
          at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:207)
          at org.jboss.arquillian.junit.Arquillian$5$1.evaluate(Arquillian.java:225)
          at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
          at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:221)
      ...
      Caused by: java.lang.IllegalStateException: Error launching test com.app.domain.GenericDaoTestCase public void com.app.domain.GenericDaoTestCase.testSave() throws java.lang.Exception
          at org.jboss.arquillian.protocol.servlet_3.ServletMethodExecutor.invoke(ServletMethodExecutor.java:61)
          at org.jboss.arquillian.impl.handler.ContainerTestExecuter.callback(ContainerTestExecuter.java:50)
          at org.jboss.arquillian.impl.handler.ContainerTestExecuter.callback(ContainerTestExecuter.java:40)
          at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:63)
          ... 30 more
      Caused by: java.lang.ClassNotFoundException: org.jboss.weld.resources.spi.ResourceLoadingException
          at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
          at java.security.AccessController.doPrivileged(Native Method)
          at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
          at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
          at java.lang.Class.forName0(Native Method)
          at java.lang.Class.forName(Class.java:247)
          ... 33 more

       

      Is there anything missing from my project, or something else was happening?

       

      Thanks.

        • 1. Re: JPA TestCase Triggered ClassNotFoundException: org.jboss.weld.resources.spi.ResourceLoadingException
          aslak

          I Assume you're seeing this Exception on the client side ?

           

          We currently only serialize the exception back to the client, so if your missing any of the Exception classes in your client classpath you will get these kinds of Exceptions. What it is saying is basically, it was thrown a ResourceLoadingException when running your test incontainer.

           

          If you add the weld-api/spi jars to your test classpath you should be able to see the real exception. It might be in the server log as well..

           

          Work in progress on fixing this:  https://jira.jboss.org/browse/ARQ-299

          • 2. Re: JPA TestCase Triggered ClassNotFoundException: org.jboss.weld.resources.spi.ResourceLoadingException
            markwen97

            Hi Aslak,

             

            You are right, the server log is very helpful.

             

            I really appreciate.

             

            Mark

            • 3. Re: JPA TestCase Triggered ClassNotFoundException: org.jboss.weld.resources.spi.ResourceLoadingException
              markwen97

              With some changes to the packaging, I finally got through the simple  test, but I won't say it's a "breeze" (as Dan touted), as you can see  from the codes:

               

              @Deployment
                 public static Archive<?> createDeployment()
                 {
                    return ShrinkWrap.create(WebArchive.class, "test.war")
                          .addClasses(Role.class)
                          .addClasses(GenericDao.class)
                          .addClasses(GenericDaoHibernate.class)
                          .addClasses(com.app.domain.model.BaseObject.class)
                          .addClasses(org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver.class)
                          .addClasses(org.springframework.instrument.classloading.LoadTimeWeaver.class)
                          .addClasses(org.springframework.security.GrantedAuthority.class)
                          .addPackage(org.springframework.orm.ObjectRetrievalFailureException.class.getPackage())
                           .addPackage(org.springframework.core.Ordered.class.getPackage())
                           .addPackage(org.springframework.context.ResourceLoaderAware.class.getPackage())
                           .addPackage(org.springframework.dao.DataAccessException.class.getPackage())
                          .addPackage(org.springframework.context.weaving.LoadTimeWeaverAware.class.getPackage())
                          .addManifestResource("test-persistence.xml", "persistence.xml")
                          .addWebResource(EmptyAsset.INSTANCE, "beans.xml");
                  }

               

              Almost  every addClasses() or addPackage() means a break during the testing, and I had to check the server log or the surefire report to find out why the test case failed. Mostly it's due to some missing class, and sometimes I had to guess what Spring wants which class. This is not  productive at all, and a quite serious issue for larger scale testing -  anyway I was just testing a primitive save operation by the entity  manager:

               

              public <T> T save(T object) {
                      return (T) getEntityManager().merge(object);
              }

               

              So  I am wondering, are there any easier ways that will let Arquillian or  ShrinkWrap pick up the classes automatically during the packaging?

              • 4. Re: JPA TestCase Triggered ClassNotFoundException: org.jboss.weld.resources.spi.ResourceLoadingException
                aslak

                a couple of tips:

                 

                - addClasses takes a array of of classes, so you can write .addClasses(Role.class, GenericDao.class)

                - addPackages takes a array of packages, so you can write .addPackages(recursive, org.MyClass.class.getPakcage())

                - addPackages support adding recursively all sub packags, .addPackages(true, org.MyClass.class.getPackage())

                 

                You can create a little ShrinkWrap 'factory/helper' of your own to add all the common stuff between your tests, you don't have to repeat the same code adding spring all over the place, that normally helps a lot..

                 

                But in the upcoming weeks we will be releasing a new ShrinkWrap extension that should help some:

                 

                ShrinkWrap Dependencies:

                http://github.com/kpiwko/shrinkwrap/blob/SHRINKWRAP-140/extension-dependencies

                 

                It will allow you to import Maven dependencies into your archive:

                 

                http://github.com/kpiwko/shrinkwrap/blob/SHRINKWRAP-140/extension-dependencies/src/test/java/org/jboss/shrinkwrap/dependencies/PomDependenciesUnitTestCase.java

                http://github.com/kpiwko/shrinkwrap/blob/SHRINKWRAP-140/extension-dependencies/src/test/java/org/jboss/shrinkwrap/dependencies/DependenciesUnitTestCase.java

                 

                It can resolve whole dependency trees, you can optionally leave out the version number and it will get pulled in from your projects pom.

                 

                e.g.

                ShrinkWrap.create(WebArchive.class).addLibaries(Dependencies.artifact("org.springframework:springframework").resolve())

                 

                 

                Also we will be adding support for addPackage based on string, so it doesn't have to be a Class in the package you want to add any more.

                .addPackages(true, "org.springframework") would pull in everything in spring found on classpath.

                 

                Have some thought about making a arquillian extension that will attempt to auto discover some imports, but it's a bit tricky and will never work 100%. Who implements this interface? Who owns this interface? is the interface/impl part of the container runtime? etc

                • 5. Re: JPA TestCase Triggered ClassNotFoundException: org.jboss.weld.resources.spi.ResourceLoadingException
                  markwen97

                  It's really exciting news, and big unloading of the burdens for serious Arquillian testing.

                   

                  Many Thanks,

                   

                  Mark