7 Replies Latest reply on Jan 13, 2008 12:26 AM by Daniel Hinojosa

    DBUnit Data "Disappearing" During SeamTest

    neilac333 Novice

      NOTE: This may be more appropriate for a different forum, but it seemed best to start here.

      I am using the DBUnit Ant task to do a clean insert of a seed file before running a SeamTest unit test. I have verified that DBUnit properly performs the insert I specify prior to the test. But for some odd reason, the data "disappears" during the test.

      To make things clearer, here are my Ant task and a fragment of my test:











      ----------------------------------

      The first 5 statements of the test (after variable declarations):

      emf = Persistence.createEntityManagerFactory("jpaPersistenceUnit");
      Assert.assertNotNull(emf, "entity manager factory null");
      em = emf.createEntityManager();
      Assert.assertNotNull(em, "entity manager null");

      current = em.find(MyClass.class, new Long(1));

      ----------------------------------

      DBUnit inserts a record with ID 1 as specified in the seed file. I verified this by running the DBUnit task without running the subsequent test. That last statement in the test, however, causes the test to fail because the entity manager is unable to find anything in the table with ID 1. And when I check the table, sure enough...the data that DBUnit inserted has vanished.

      Why is this happening? Please let me know if you need more information.

      Thanks for any insight.

        • 1. Re: DBUnit Data
          neilac333 Novice

          For some reason, the Ant target didn't come through. Here it is again:

          <target name="run-unit-tests" depends="clean-test-output" >
           <ant antfile="build-persistence-jar.xml" target="jar-with-tests"/>
           <dbunit driver="${database.driver.class}"
           url="${database.url}"
           userid="${database.username}"
           password="${database.password}"
           classpathref="database.class.path.libs">
           <operation type="CLEAN_INSERT" src="${dataset.dir}/insertTwo.xml"/>
           </dbunit>
           <testng classpathref="tests.class.path.libs" outputDir="${test.report.dir}" failureProperty="test.failed">
           <xmlfileset dir="${test.suite.dir}"/>
           </testng>
          </target>
          
          


          • 2. Re: DBUnit Data
            Pete Muir Master

            Have you seen that we have support for DBUnit SeamTests in Seam 2.0.0.GA? Its super easy to use (see the seamdiscs tests for an example, and its documented in the testing chapter).

            Actually, I've pretty much given up trying to write persistence tests without it.

            I also want to try to get some support for DBUnit into HibernateTools/seam-gen.

            • 3. Re: DBUnit Data
              neilac333 Novice

              Actually, I did notice that in the wiki example. I gave DBUnitSeamTest a quick shot because of my disappearing data situation doing things the conventional way, but I kept getting a MalformedURLException wrapped in a DBUnit DatasetException. I was probably doing something silly with the path to my seed file. I can explore further in the morning when I get in the office.

              I will let you know what happens, and maybe my experience will help others in the forum as well.

              Thanks.

              • 4. Re: DBUnit Data
                neilac333 Novice

                I'm still getting the same exception:

                java.lang.RuntimeException: org.dbunit.dataset.DataSetException: java.net.MalformedURLException
                 at org.jboss.seam.mock.DBUnitSeamTest$DataSetOperation.<init>(DBUnitSeamTest.java:170)
                 at org.jboss.seam.mock.DBUnitSeamTest$DataSetOperation.<init>(DBUnitSeamTest.java:159)
                 at com.myorg.myproj.persistence.test.UpdatableEntityTest.prepareDBUnitOperations(Unknown Source)
                 at org.jboss.seam.mock.DBUnitSeamTest.init(DBUnitSeamTest.java:112)
                 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                 at java.lang.reflect.Method.invoke(Method.java:585)
                 at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:604)
                 at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:394)
                 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
                 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:79)
                 at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:165)
                 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:103)
                 at org.testng.TestRunner.runWorkers(TestRunner.java:678)
                 at org.testng.TestRunner.privateRun(TestRunner.java:624)
                 at org.testng.TestRunner.run(TestRunner.java:495)
                 at org.testng.SuiteRunner.runTest(SuiteRunner.java:300)
                 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:295)
                 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:275)
                 at org.testng.SuiteRunner.run(SuiteRunner.java:190)
                 at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:792)
                 at org.testng.TestNG.runSuitesLocally(TestNG.java:765)
                 at org.testng.TestNG.run(TestNG.java:699)
                 at org.testng.TestNG.privateMain(TestNG.java:824)
                 at org.testng.TestNG.main(TestNG.java:802)
                Caused by: org.dbunit.dataset.DataSetException: java.net.MalformedURLException
                 at org.dbunit.dataset.xml.FlatXmlProducer.produce(FlatXmlProducer.java:169)
                 at org.dbunit.dataset.CachedDataSet.<init>(CachedDataSet.java:71)
                 at org.dbunit.dataset.xml.FlatXmlDataSet.<init>(FlatXmlDataSet.java:200)
                 at org.dbunit.dataset.xml.FlatXmlDataSet.<init>(FlatXmlDataSet.java:187)
                 at org.jboss.seam.mock.DBUnitSeamTest$DataSetOperation.<init>(DBUnitSeamTest.java:168)
                 ... 25 more
                


                I assume this means that the location I am passing to the DataSetOperation constructor isn't "right." Here is what I am doing:

                beforeTestOperations.add(
                 new DataSetOperation("com/myorg/myproj/persistence/test/SRM_USER_Two.xml")
                 );
                


                The seed file above is in the folder described, and I even tried the absolute path (i.e. C:\...) to no avail.

                Any insight into how to exectue the DBUnit Seam test properly is appreciated.

                And even if I get that working, I would absolutely love to know why my original Ant configuration has the data "disappear." It may be merely academic, but I am really curious about the reason for this bizarre behavior.

                Thanks.

                • 5. Re: DBUnit Data
                  neilac333 Novice

                  This has nothing to do with the exception at hand, but I saw Christian wrote that certain methods in DBUnitSeamTest are tailored to HSQLDB. See here:

                  http://docs.jboss.com/seam/2.0.0.CR3/api/org/jboss/seam/mock/DBUnitSeamTest.html

                  However, I am using Oracle. Once I resolve this exception, with the help of this forum of course, is DBUnitSeamTest still viable for me without overriding the methods Christian mentions?

                  Thanks.

                  • 6. Re: DBUnit Data
                    neilac333 Novice

                    As Stewie on Family Guy might say, "Victory is mine!"

                    I discovered why the data was disappearing. As I said, the DBUnit Ant task was populating the database as I wanted, but something was happening when I ran my SeamTest, which retrieved that data through JPA. Well, the persistence unit through which I created the entity manager had hibernate.hbm2ddl.auto set to "create-drop." Just one setting among many. Once I set it to "update," everything worked as it should. This goes to show how the littlest, seemingly unrelated thing can have profound side effects.

                    Of course, I still have no idea how to get my DBUnitSeamTest past that exception, but at least now I can test my code the old-fashioned way!

                    Thanks.

                    • 7. Re: DBUnit Data
                      Daniel Hinojosa Master

                      I had the same exception. It was just misconfiguration.

                      If you are using seam-gen
                      Check: persistence-test.xml and make sure that the jta-data-source is correct as specified by the bootstrap. Make sure that the jboss.entity.manager.factory.jndi.name matches on the persistence-test.xml and the components.xml. Also make sure that in the test-build directory you actually see the data xml file.