views:

103

answers:

1

Why is there so much hating going on about 'partial mocking' and the code that requires it?

Here's an (theoretical) example implementation:

public ComplexResult1 operationA(Stimulus a) {
    {
        ...
        result = ...;
    }
    auditTheChange(a);
}
public ComplexResult2 operationB(Stimulus b) {
    {
        ...
        result = ...;
    }
    auditTheChange(b);
    return result;
}
void auditTheChange(Stimulus stim) {
    // do a bunch of stuff to record the change
    // and interact with another outside service
}

Now, in my understanding this is well refactored code.

If I want to UNIT test operationA and operationB, and ensure that auditing happens in each scenario, but without having to test the specifics of the audit code, I would use partial mocking.

What am I not seeing/understanding that causes so many projects (EasyMock, Mockito, etc.) to recommend refactoring?

+2  A: 

If auditing is truly an internal function of the class then the code should be tested as part of the unit test. Why does your class handle both complex operations and auditing? Could the auditing be moved to a separate class?

If so, introduce the auditing as a collaborator with this class and mock it out. If not, unit test it.

You can use partial mocks, but in this case I think it is an indication the class is doing too much.

emulcahy