DBUnit deleting correct records

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

DBUnit deleting correct records

I have a unit test that inserts rows, and I'd like DBUnit to automatically clean up those rows afterwards (DBUnit does not initially insert those rows - that's purely the responsibility of the code being tested). The table whose rows are being inserted have multiple columns, and a 1-column primary key. I've defined the XML dataset as the following, even though there are multiple columns in tableTest:

  <tableTest columnA="10001" />

However, without having specified to DBUnit anywhere that columnA is the primary key, my JUnit test method annotated with @After does a DatabaseOperation.DELETE.execute(...) and successfully removes the added in row! How does it do this? I figured it might use JDBC metadata, but that's not mentioned anywhere in the DBUnit documentation.

I figured maybe it removes every column that matches the values in the XML file (i.e. delete from tableTest where columnA="10001"). But when I tried the above test specifying only a non-primary key instead of columnA in the XML file, I get the following:

org.dbunit.dataset.NoSuchColumnException: tableTest.columnA
        at org.dbunit.dataset.AbstractTable.getColumnIndex(AbstractTable.java:74)
        at org.dbunit.dataset.DefaultTable.getValue(DefaultTable.java:170)
        at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:177)
        at MyTest.callTearDown(MyTest.java:10)

So what is going on - how does DBUnit know what rows(s) to remove, when it doesn't insert those same rows before that test executes?