Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have a system composed of three classes: A,B,C that share certain dependencies.

To test A independently, I would need to create mock objects for B and C To test B independently, I would need to create mock objects for A and C etc...

So if I want to write tests for A, B, and C, I would need to create mock classes for each one of them. Am I understanding this correctly?

share|improve this question
up vote 3 down vote accepted

I interpret your example to mean A, B, and C are all interdependent, i.e. there are six edges in the dependency graph. Before considering how to unit test such a design, you might ask yourself whether you really need that many dependencies. It's a nice idea for a society but not for a code base. A large number of interdependencies is a forbearer of brittle, hard-to-maintain code.

share|improve this answer

The answer is yes, but this is often overkill. Ideally your application should have been programmed to interfaces and you should be able to choose to mock out the interfaces that allow you to test components independently.

share|improve this answer
+1 Using interfaces decouples the application making it robust enough for future changes. It also follows the "Law of Demeter" design principle eliminating dependencies – Aruna Jun 8 '11 at 22:39

One would want to mock dependencies. If A depended (ie used) B and C, one might need to mock B and C depending upon whether or not that particular test uses them.

The creation of a mock is also not a one-time thing. Mocks are used to test expectations and force certain behavior (eg function throwing an exception in order to test exception handling).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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