Search code examples
hibernatetestingdbunit

How to avoid conflict with ids inserted by DbUnit and entities inserted by Hibernate


I want to use DbUnit to test JPA/Hibernate Dao, but since DbUnit XML files contain hard-coded ids for my entities, i have problems when testing inserts done by Hibernate.

Do you know any DbUnit extension or solution which would use the PK Generator defined on my entities when inserting data from XML files ? or even insert Data with Hibernate itself ?

other strategies ?

rgds


Solution

  • For your hardcoded Ids in the test data you can either use negative Ids, or very high Ids, for example counting down from Integer.MAX_VALUE.

    You could also use a custom Id generator. However this would require replacing the annotation for entities under test on build. You can find more details here