I'm trying to write some unit tests for a JPA model that I've built on top of a legacy database. Some of the object graphs in my model are large and I would like to find a way to write the unit tests such that I can test a single object in the graph (i.e. instantiate new object, populate with data, persist, refresh, compare refreshed object members with original) in isolation without having to fully populate the object graph.
My initial thought was that I could use mock objects for any referenced objects in the object under test but I don't think this will be sufficient being that the referential integrity of the schema will not allow the objects that are represented by the mock objects to be inserted without being populated with data.
I guess what I am really looking for is a way to instantiate the object under test and then have the rest of the object graph populated somehow with data that I don't really care about but that still maintains the referential integrity of the schema.
I know there are many options out there for Java unit testing including DbUnit, EasyMock, Unitils etc. but I'm not sure exactly what combination (or features) I should use to solve this problem. Can someone point me in the right direction? Feel free to criticize my approach to the problem as well; I may need to restructure that.
You might want to look at the Object Mother or Test Data Builder patterns;
The TestDataBuilder pattern allows tests to specify only those parts of the objects that need to vary and use sensible defaults for those that are not relevant to the test.
see also: