But my question is in large projects where each class depends on many other classes, how do you go about device class testing?
First, you understand that "every class depends on many other classes" - this is bad, right? And that this is not a function of a large project, but a poor design? (This is one of the advantages of TDD, which tends to discourage such highly connected code.)
Interrupting any other class does not make sense because of both the complexity and the time required to write the stubs.
Not to mention that this does not help design problems. Worse, investing in all stubs can actually be an obstacle to refactoring, if only psychological.
The best approach (IMHO) is to start inside out at the classrooms, changing the design as you go. I usually approached this by doing what I call "internal dependency." This includes leaving the method signatures intact, but retrieving the data needed from the dependencies, and then retrieving the functions that contain the actual logic. A trivial example might be:
public int foo(A a, B b) { C c = a.getC(); D d = b.getD(); Bar bar = calculateBar(c, d); return calculateFoo(bar, this.baz); } Bar calculateBar(C c, D d) { ... } int calculateFoo(Bar bar, Baz baz) { ... }
Now I can write a test to calculate Bar and calculateBaz, but I do not need to worry about setting up internal states (this.baz), and I do not need to worry about creating mock versions of A and B.
After creating such tests using existing interfaces, I look for an opportunity to push changes to the interface, for example, change the Foo method to use C and D, rather than A and B.
Does any of this make sense?
Jeffrey fredrick
source share