DBUnit Data "Disappearing" During TestNG Test

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

DBUnit Data "Disappearing" During TestNG Test

Neil Chaudhuri
I am using the DBUnit Ant task to do a clean insert of a seed file before running a TestNG unit test (which incidentally runs a class that extends SeamTest from the JBoss Seam project).  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:

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

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

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");

currentUser = 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.
 


Peek-a-boo FREE Tricks & Treats for You! Get 'em!
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: DBUnit Data "Disappearing" During TestNG Test

John Hurst-2
Hi,

I'd try it with an empty TestNG test that does nothing, and see
whether the Ant task then results in data in the database.

If the <dbunit> CLEAN_INSERT by itself leaves data in the database,
but <dbunit> CLEAN_INSERT followed by <testng> does not, then
something in the <testng> is removing the data, right?

I am assuming you have verified the data left by <dbunit> CLEAN_INSERT
in a separate session -- meaning that the <dbunit> data were
definitely committed.  Some people use DbUnit to prime the database,
and then continue with the test in the same transaction, rolling back
afterwards. This makes tests faster, but does mean you generally
cannot look at the data after a test (because the changes are rolled
back). However, I don't think this approach is possible with the Ant
task for CLEAN_INSERT.

John Hurst
Wellington, New Zealand

On Nov 6, 2007 8:17 AM, Neil Chaudhuri <[hidden email]> wrote:

>
>  I am using the DBUnit Ant task to do a clean insert of a seed file before
> running a TestNG unit test (which incidentally runs a class that extends
> SeamTest from the JBoss Seam project).  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:
>
> <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>
>
> ----------------------------------
>
> 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");
>
> currentUser = 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.
>
>
> ________________________________
> Peek-a-boo FREE Tricks & Treats for You! Get 'em!
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems?  Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
>

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user