For unit testing is it better to mock the data layer or use an embedded database like derby?
I know that it also depends on the purpose of the testing. But if I go with derby I don’t have to mock all the objects and I assume that would be easier. On the other hand I understand that that is more towards integration testing. So which one is more common for unit testing?
Thanks.
Update according to comments:
So I have derby configured now but my manager insists on using easymock. We are using jpa and we have about 20 tables => data models. So then for each method like for project model should I specify the return type of the mockedProject for all its methods? Like getProjectName(), getProjectId(), etc? And also should I mock the persistent manager object. I thought that is just a lot in comparison with just configuring an embeded db like derby.
If you’re using JPA, you don’t need to mock your Project objects, because they are probably just dumb POJO’s, right? What you need to mock is just the persistence manager object (EntityManager). In the simplest case (if you’re using Mockito or Easymock with ‘niceMock’) you may just need to create the mock and inject it and that’s it. Depending on what you’re testing, you will probably want to do more than that: verifying that a
saveormergemethod is called, specifying that it is to return a particular Project object on agetcall, etc.Mocking the EntityManager has several benefits:
Mocking a POJO has none of these benefits. It’s just as fast to execute the code of a POJO as a mocked POJO. Populating a POJO can be a bit of a pain, but not as much as setting up the behavior of a mocked POJO. And since POJO’s don’t (normally) have much in the way of external dependencies, the third benefit is rarely required.