DbUnit: problem with increment id generation
Asked Answered
W

2

2

I am using DbUnit together with Unitils, which works great most of the time.

Today I found a strange problem.

Situation is:

  • I use Hibernate, and have id with "increment" generator:
<id name="Id">
   <generator class="increment"/>
</id>
  • I prepare test dataset, where maximal id is 5.
  • I use clean-insert loading strategy.
  • I have two test methods test1 and test2, each adding one row in this table.
  • After test1 method newly added row has id=6.
  • After test2 method newly created row has id=7.

This is all OK and I get why this is like that. It is a problem from maintenance perspective though. If I ever add third test method between the two, method test2 will suddenly fail even though nothing changed, just because row will get different id.

Is there anyway I can force DbUnit or Hibernate to calculate next id value before each test method?

Wallinga answered 9/2, 2010 at 9:49 Comment(0)
M
3

First thing, you should provide the complete dataset, yes with id as well. If not, don't test or base your test upon ids. Why not test? because its already well tested and reliable thing. Always remember never test third party libraries, most of them already well tested. But it seems quite impossible to not depending upon ids. I agree, you should write some mock class to tackle this issue for you, or may be you can provide some setter method to overwrite the value generated by your own.

Second option is always start your test case with empty table. You can write a fixture to clean the table for you, before every test case.

Maestricht answered 9/2, 2010 at 10:20 Comment(1)
Cleaning the table is normally not enough; you may also have to reset the table id sequence.Easygoing
P
3

The solution is not to rely on generated ids:

  • they are outside the control of your test.
  • if you make them controlled by the test, you are no longer testing the class-under-test
Prognostication answered 9/2, 2010 at 10:6 Comment(4)
How can I do this? Is there a way to ignore key columns somehow?Wallinga
I don't know, I have no information about your test class and class-under-testPrognostication
I meant, how to do this in DbUnit and Unitils?Wallinga
I think I implied "don't do it" ;) Read Vinegar's answer for better understanding.Prognostication
M
3

First thing, you should provide the complete dataset, yes with id as well. If not, don't test or base your test upon ids. Why not test? because its already well tested and reliable thing. Always remember never test third party libraries, most of them already well tested. But it seems quite impossible to not depending upon ids. I agree, you should write some mock class to tackle this issue for you, or may be you can provide some setter method to overwrite the value generated by your own.

Second option is always start your test case with empty table. You can write a fixture to clean the table for you, before every test case.

Maestricht answered 9/2, 2010 at 10:20 Comment(1)
Cleaning the table is normally not enough; you may also have to reset the table id sequence.Easygoing

© 2022 - 2024 — McMap. All rights reserved.