Assume that in a database table there is a non-key column called RECORD_ID which is auto-generated/sequenced in database.
Now in my entity for the table above, just above the field for RECORD_ID, I use the hibernate annotation @Generated to indicate to hibernate that DB will take responsibility for inserting value into that particular field. It looks like the following:
@Column(name = "RECORD_ID", unique = true, nullable = true, insertable = false, updatable = false)
@Generated(GenerationTime.INSERT)
private Long recordId;
I run my code and it works fine. DB is inserting value as expected.
Now, I write a unit-test case with HSQL to ensure my logic is intact.
The logic has a query which fetches the record based on RECORD_ID.
Now when i create fixture and invoke my test, HSQL will not generate value for the record’s RECORD_ID when I save my fixture. It leaves it as null. But the logic that I intend to test is based on fetching by RECORD_ID which isn’t possible here.
It just remains null in HSQL DB. And hence I am not able to complete unit testing!
Kindly throw some light on solving this issue or any workaround.
Please find the unit-test code below
@Test
public void testProductCancelDAL() {
savePrerequisites();
Logic myLogic = new Logic();
/* Logic fetches the reservation record based on RECORD_ID */
assertNotNull(myLogic.invoke());
}
public void savePrerequisites() {
//Stroing product type
Product product = ProductFixture.createProduct();
sessionFactory.getCurrentSession().save(product);
//Storing Itinerary
Itinerary itn = ItnFixture.create();
sessionFactory.getCurrentSession().save(itn);
Reservation res = ReservationFixture.create();
sessionFactory.getCurrentSession().save(res);
sessionFactory.getCurrentSession().flush();
}
Based on what you said in the comments, it appears that you’re testing some code which depends on a trigger in the database generating the RECORD_ID, but that you test this on an in-memory database where this trigger doesn’t exist. This, obviously, won’t work. You need to create the same trigger as in your production database. See http://hsqldb.org/doc/2.0/guide/triggers-chapt.html to create triggers in a HSQLDB database.