6 Replies Latest reply on Jul 11, 2014 9:46 AM by Bartosz Majsak

    Struggling with JPA/DBUnit

    Nicklas Karlsson Master

      Hi,

       

        I'm trying to build a full concept project that covers business logic, the UI layer and DB layers and I'm having some problems with DBUnit. I'm on alpha7 + WF8CR2.

       

      <?xml version="1.0" encoding="UTF-8"?>
      <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>fi.affecto.marela</groupId>
        <artifactId>arq-concept</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>
      
      
        <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <browser>chrome</browser>
        </properties>
      
      
        <dependencyManagement>
        <dependencies>
        <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>1.1.4.Final</version>
        <scope>import</scope>
        <type>pom</type>
        </dependency>
        <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-drone-bom</artifactId>
        <version>1.3.0.Final</version>
        <type>pom</type>
        <scope>import</scope>
        </dependency>
        <dependency>
        <groupId>org.jboss.arquillian.selenium</groupId>
        <artifactId>selenium-bom</artifactId>
        <version>2.41.0</version>
        <type>pom</type>
        <scope>import</scope>
        </dependency>
        </dependencies>
        </dependencyManagement>
      
      
        <dependencies>
        <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>4.3.5.Final</version>
        <scope>provided</scope>
        </dependency>
        <dependency>
        <groupId>org.jboss.arquillian.graphene</groupId>
        <artifactId>graphene-webdriver</artifactId>
        <version>2.0.2.Final</version>
        <type>pom</type>
        <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-persistence-core</artifactId>
        <version>1.0.0.Alpha7</version>
        </dependency>
        <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-persistence-dbunit</artifactId>
        <version>1.0.0.Alpha7</version>
        </dependency>
        </dependencies>
      
      
        <profiles>
        <profile>
        <id>arq-wildfly</id>
        <dependencies>
        <dependency>
        <groupId>org.jboss.spec</groupId>
        <artifactId>jboss-javaee-7.0</artifactId>
        <version>1.0.0.Final</version>
        <type>pom</type>
        <scope>provided</scope>
        </dependency>
        <dependency>
        <groupId>org.wildfly</groupId>
        <artifactId>wildfly-arquillian-container-remote</artifactId>
        <version>8.1.0.CR2</version>
        <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.jboss.arquillian.protocol</groupId>
        <artifactId>arquillian-protocol-servlet</artifactId>
        <scope>test</scope>
        </dependency>
        </dependencies>
        </profile>
        </profiles>
      
      
      
      
        <build>
        <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
        <source>1.7</source>
        <target>1.7</target>
        </configuration>
        </plugin>
        </plugins>
        <testResources>
        <testResource>
        <directory>src/test/resources</directory>
        <filtering>true</filtering>
        </testResource>
        </testResources>
        </build>
      </project>
      

       

       

      <arquillian>
        <extension qualifier="webdriver">
        <property name="browser">${browser}</property>
        <property name="chrome.binary">C:/Progra~2/Google/Chrome/Application/chrome.exe</property>
        <property name="chromeDriverBinary">c:/java/tools/chromedriver.exe</property>
        </extension>
          <extension qualifier="persistence">
              <property name="defaultDataSource">java:jboss/datasources/ExampleDS</property>
          </extension>
      </arquillian>
      

       

      package fi.affecto.marela.test;
      
      
      import javax.inject.Inject;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.transaction.UserTransaction;
      
      
      import org.jboss.arquillian.container.test.api.Deployment;
      import org.jboss.arquillian.junit.Arquillian;
      import org.jboss.arquillian.persistence.ShouldMatchDataSet;
      import org.jboss.arquillian.persistence.UsingDataSet;
      import org.jboss.arquillian.transaction.api.annotation.Transactional;
      import org.jboss.shrinkwrap.api.Archive;
      import org.jboss.shrinkwrap.api.ShrinkWrap;
      import org.jboss.shrinkwrap.api.asset.EmptyAsset;
      import org.jboss.shrinkwrap.api.spec.JavaArchive;
      import org.junit.Assert;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      
      
      import fi.affecto.marela.jpa.Game;
      
      
      @RunWith(Arquillian.class)
      public class PersistenceExtensionTest
      {
      
      
         @Deployment
         public static Archive<?> createDeployment()
         {
            return ShrinkWrap.create(JavaArchive.class, "test.jar").addPackage(Game.class.getPackage())
               .addAsManifestResource("persistence.xml", "persistence.xml")
               .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
         }
      
      
         @PersistenceContext
         EntityManager em;
      
      
         @Inject
         UserTransaction utx;
      
      
         @Test
         public void testX()
         {
            Assert.assertTrue(false);
         }
      
      
         @Test
         @UsingDataSet("games.yml")
         @ShouldMatchDataSet("games-check.yml")
         @Transactional
         public void testSeeding() throws Exception
         {
            Assert.assertTrue(false);
         }
      
      
      }
      

       

      For some strange reason, the testSeeding succeeds! Theories?

        • 1. Re: Struggling with JPA/DBUnit
          Roman Besolov Newbie

          Look here: Injecting EJB into tests

          Maybe you have injection problem.

          • 2. Re: Re: Struggling with JPA/DBUnit
            Nicklas Karlsson Master

            Don't think so. I have

             

            package fi.affecto.marela.test;
            
            
            import javax.inject.Inject;
            import javax.persistence.EntityManager;
            import javax.persistence.PersistenceContext;
            import javax.transaction.UserTransaction;
            
            
            import org.jboss.arquillian.container.test.api.Deployment;
            import org.jboss.arquillian.junit.Arquillian;
            import org.jboss.arquillian.persistence.ShouldMatchDataSet;
            import org.jboss.arquillian.persistence.UsingDataSet;
            import org.jboss.arquillian.transaction.api.annotation.Transactional;
            import org.jboss.shrinkwrap.api.Archive;
            import org.jboss.shrinkwrap.api.ShrinkWrap;
            import org.jboss.shrinkwrap.api.asset.EmptyAsset;
            import org.jboss.shrinkwrap.api.spec.WebArchive;
            import org.junit.Assert;
            import org.junit.Test;
            import org.junit.runner.RunWith;
            
            
            import fi.affecto.marela.jpa.Game;
            
            
            @RunWith(Arquillian.class)
            public class PersistenceExtensionTest
            {
            
            
               @Deployment
               public static Archive<?> createDeployment()
               {
                  return ShrinkWrap.create(WebArchive.class, "test.war").addPackage(Game.class.getPackage())
                     .addAsResource("persistence.xml", "META-INF/persistence.xml")
                     .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");     
               }
            
            
               @PersistenceContext
               EntityManager em;
            
            
               @Inject
               UserTransaction utx;
            
            
               @Test
               public void testA() throws Exception
               {
                  Assert.assertNotNull(em);
                  utx.begin();
            //      em.joinTransaction();
                  Game g = new Game();
                  g.setId(666L);
                  g.setTitle("x");
                  Assert.assertFalse(em.contains(g));
                  g = em.merge(g);
                  Assert.assertTrue(em.contains(g));
                  utx.rollback();
               }
              
               @Test
               @Transactional
               public void testB() throws Exception
               {
                  Assert.assertNotNull(em);
                  Game g = new Game();
                  g.setId(666L);
                  g.setTitle("x");
                  Assert.assertFalse(em.contains(g));
                  g = em.merge(g);
                  Assert.assertTrue(em.contains(g));
               }
            
            
            
            
               @Test
               @UsingDataSet("games.yml")
               @ShouldMatchDataSet("games-check.yml")
               @Transactional
               public void testC() throws Exception
               {
                  Assert.assertTrue(false);
               }
            
            
            }
            
            

             

            The strange things are

             

            a) testC should fail altogether

            b) Why does testA work without the em joining the transaction?

            c) Why does both testA and testB say that I'm passing in a detached instance if I use persist instead of merge?

             

            ===EDIT===

            Apparently

             

               @Test
            //   @UsingDataSet("games.yml")
            //   @ShouldMatchDataSet("games-check.yml")
               @Transactional
               public void testC() throws Exception
               {
                  Assert.assertTrue(false);
               }
            

             

            Makes testC fail corretly

            • 3. Re: Struggling with JPA/DBUnit
              Robert Panzer Newbie

              Arquillian 1.1.4.Final seems to have an issue when tests fail before they can be executed, like DI errors or the test class cannot be loaded etc.

              In this case the test seems to succeed although no method was called at all. (You can also look at [ARQ-1758] Test passes although it fails unexpectedly - JBoss Issue Tracker )

               

              If you try 1.1.3.Final at least testC() should fail. (I did not try to understand the other two tests but if they pass unexpectedly they will probably fail as well.)

               

              I just don't know if there are any dependencies fom APE Alpha 7 to Arquillian Core 1.1.4.Final that would cause other problems when using 1.1.3.Final.

               

              == EDIT

               

              Maybe APE just can't find the files referred to by the annotations or the files contain errors.

              • 4. Re: Struggling with JPA/DBUnit
                Nicklas Karlsson Master

                Thanks for the tip. Switching to 1.1.3 gave me a

                 

                org.jboss.arquillian.persistence.dbunit.exception.DBUnitInitializationException: Unable to load data set from given file: datasets/games.yml

                 

                Still no idea why the em thinks a freshly created object is detached in testA and testB, though...

                • 5. Re: Struggling with JPA/DBUnit
                  Bartosz Majsak Master

                  Hi Niklas,

                   

                  I'm on holidays at the moment so I won't be able to look at the "detached" problem as I don't have an IDE ;-)

                   

                  As for the datasets this can be due to jmx protocol. If you switch to servlet either through arq.xml or annotation it should solve it.

                   

                  I will be back in a bit more than a week so expect some updates from my side then.

                   

                  For the swallowed exception it is an issue with core 1.1.4

                   

                  Cheers,

                  Bartosz

                   

                  (this forum is quite challenging for mobile...)

                  • 6. Re: Struggling with JPA/DBUnit
                    Bartosz Majsak Master

                    Nicklas Karlsson did you try with Servlet protocol? Just wondering if i should investigate this problem deeper.

                    BTW 1.1.5.Final should improve exception handling.