I’m new to testing in Grails, so I’m not sure if I’m taking the correct approach. I am trying to unit test a service (call it FooService) that has an instance of another class (call it Bar) as a property. Basically something like:
class FooService {
Bar bar
void afterPropertiesSet() {
bar = new Bar()
}
}
So, I’m trying to test the afterPropertiesSet method, and as I understand it, the correct practice for unit tests is to mock outside classes. So, how, in FooServiceTests extends GrailsUnitTestCase, do I mock the Bar constructor?
Thanks
You don’t.
EasyMock depends on proxies, and that means an interface. Unless Bar can be an interface, it’s not a candidate for proxying.
You’d mock something like a DAO or another external dependency that you’ve already unit tested to keep your FooServiceTest from becoming an integration test, but Bar? No.
“…the correct practice for unit tests is to mock outside classes…” – I believe this is incorrect. Mocking for every object takes the idea too far.