Hi. I'm thinking that any class of reasonable complexity should be 'interfaced out', so that it can be completely removed as a dependancy in unit tests, and then tested itself in as much isolation as possible.
Eg, I have a session bean which does some stuff. It uses a validation class to do a load of validation. Would it make sense to make the validator implement an interface so that i can test it seperately, significantly simplifying the unit tests for the session bean?
The reason I ask is that thinking like this could produce alot of interfaces, but I think that the extra overhead of maintaining these interfaces will be massively outweighed by the benefits to unit testing.
Aaron, I tend to use the rule of thumb of "mock out anything that accesses an external resource or is hard to test."
By this logic, I would mock out a call to a data access object or processor, but not a validator. Unless it's really hard to pass in valid input, mocking it doesn't add any extra value for me. Regardless, I would test the validator separately. That's separate from whether to use an interface though. It's more of a statement that I'd rather test the happy path for the validator without mocks.
I'm not a unit testing purist though. I want my unit tests to test as much as possible without going overboard.
Originally posted by Jeanne Boyarsky: I tend to use the rule of thumb of "mock out anything that accesses an external resource or is hard to test."
By this logic, I would mock out a call to a data access object or processor, but not a validator. Unless it's really hard to pass in valid input, mocking it doesn't add any extra value for me.
Another reason for me would be to make the tests less brittle. That is, if I have to write a lot of tests for the session bean that need to pass in valid input, all those tests might break when the validation rules change - although the session bean still works as expected. Using a mock validator for the session bean tests would be one solution to that problem.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus