1

I just got the Mockito framework for Java to try out mocking with JUnit. Trying it brought me to a basic question that I am sure is obvious but I have had trouble finding any answer for, probably because I don't have a concise way of asking it.

Say I have a class A that contains and uses instances of Class B. Now I can mock B and pass the mocked object to class A instance all is well and good, coupling is removed from my unit test. However, now if Class B changes in a way that breaks Class A, how will I know that occurred, since my mocked class B will still perform the same way? It kind of seems like you would want the Class A tests to fail to indicate that the change in B requires a change in A as well.

Is it just that you shouldn't mock in composition scenarios?

1 Answer 1

2

I think the same scenario could happen even if class A uses class B but does not contain class B.

One way to mitigate that risk is to write unit tests for class B that validate the assumptions embedded in your mock.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.