DbUnit: issue with increment id generation

I use DbUnit along with Unitils, which works great most of the time.

Today I found a strange problem.

Situation:

  • I use Hibernate and have an identifier with a "increment" generator:
  <id name = "Id">
    <generator class = "increment" />
 </id>
  • I am preparing a test data set where the maximum id is 5.
  • I use a clean insert loading strategy.
  • I have two test methods test1 and test2 , each of which adds one row to this table.
  • After the test1 method, the newly added row has id = 6.
  • After the test2 method, the newly created line has id = 7.

Everything is in order, and I understand why this is so. However, this is a problem in terms of service. If I ever add a third testing method between them, test2 will end unexpectedly, even if nothing changes, simply because the string will get a different identifier.

Anyway, can I get DbUnit or Hibernate to calculate the next id value before each test method?

+2
java unit-testing junit hibernate dbunit
source share
2 answers

First, you must provide a complete dataset, yes with id . If not, do not test or base your test on ids . Why not check out? because its already a well-proven and reliable thing. Always remember, never check third-party libraries, most of them are already well tested. But it seems completely impossible independent of id s. I agree, you should write some layout to solve this problem, or maybe you can provide some setter method to overwrite the value generated by your own.

The second option always runs a test case with an empty table. You can write an instrument to clear the table for you, before each test case.

+3
source share

The solution should not rely on the generated identifiers:

  • they are out of control of your test.
  • if you control them by testing, you no longer test the class test
+3
source share

All Articles