springtestdbunit / spring-test-dbunit

Integration between the Spring testing framework and DBUnit
http://springtestdbunit.github.com/spring-test-dbunit/
Apache License 2.0
475 stars 238 forks source link

Error with multiple source files containing different tables. #27

Closed TheLudd closed 10 years ago

TheLudd commented 11 years ago

I have two xml files containing data for different tables. I get an error when I run multiple tests using CLEAN_INSERT as DatabaseOperation.

I have traced the error to the method setupOrTeardown in class DbUnitRunner. The outer loop runs once when a test is setup since a @DatabaseSetup annotation is detected on my class. The inner loop then runs twice, one for each xml file. The first test always passes but when it is time for the second test this will happend: 1 In the first iteration of the inner loop dbUnitDatabaseOperation will be a CompositeOperation, clean and insert, and it will execute. 2 lastOperation will be set to CLEAN_INSERT 3 The second iteration of the inner loop will execute now working with the sexcond xml file. 4 Since lastOperation is CLEAN_INSERT dbUnitDatabaseOperation will now become an InsertOperation and not a CompositeOperation. 5 The data in the second file will be inserted before the tables in that file has been cleared. 6 A unique constraint error is thrown.

I cloned the repository and tried to recreate this error in the tests but I failed even If I verified through debugging that at least step 1-4 is the same. Somehow, no error is thrown. I don't know what can differ from my setup and the test setup. The only thing I found was that the tests let hibernate automatically create and drop table. I do not do this in my setup. My tables are created through generated sql files.

philwebb commented 10 years ago

Are you still seeing this issue? Is it possible to create a project to reproduce it?

wilhelmi commented 10 years ago

I have been running into this as well, I have a little sample app to demo the problem.

Example project is here: https://github.com/wilhelmi/dbunit-example

There is a test class with 2 identical test methods. I would expect both to pass, however only the first passes, the second fails with a primary key failure.

izerui commented 10 years ago

wait resolved!

mutyonok commented 10 years ago

I've added a test that reproduces the issue

possible workaround is to mention in first file all tables that are in other files and need to be clear

possible fix - programmatically merge all setup files into one before process it by DbUnit

wilhelmi commented 10 years ago

I started looking into merging the setup files into one operation here if you want to take a look:

https://github.com/wilhelmi/spring-test-dbunit

mutyonok commented 10 years ago

Hi wilhelmi,

I've tested your fix and it worked for me - my test passed. Though some test are now broken :) will look at them

We now fail test when @DatabaseSetup is mix-used on class and test method - I'm not sure about whether this test was correct anyway: when we have CLEAN_INSERT setup both on class and test method - previously second setup script was inserted instead of clean_inserted.

mutyonok commented 10 years ago

I've created a pull request that fixes the issue @philwebb please check this (https://github.com/springtestdbunit/spring-test-dbunit/pull/56)!