Question

I have a class with a primary key that is stored in a database. When creating a new instance of the class it can either be fetched from the HSQLDB database or for items not in the database it inserts everything but the primary key and this is autogenerated and returned to the class to set the id attribute. I then have a getID() method to retrieve this ID.

I am trying to write a JUnit test that checks to ensure the generated ID is what is returned by the getID() method but I have already used 'CALL IDENTITY' query to retrieve the ID inside the constructor and subsequent uses of this return 0. What is the best way of ensuring the generated ID is the same as the id returned by the getID() method?

Was it helpful?

Solution

I would really consider the different design to to test the classes. Accessing a database really isn't a unit test in most cases. If the db ID generator works and has a test then you shouldn't really need it to test your class.

Provided that you class can return an Id then all that should matter it that your class returns the id that was put into it in at the right time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top