Friday, December 11, 2009

Is this a bad design?

Suppose we have this class.

public class Class1{
public int Foo(){
Class2 c2 = new Class2();
int x = c2.Bar();
return c2.Baz(x);
}
}


Seems simple enough, right? Now the question: how do you unit test this? Like, really unit test it. Assume that Bar and Baz hit a database or something. You want to write a test to _just_ make sure that that those methods are called. You can't really mock out Class2. Yet this meets even one of the strictest standards of coding, the Law of Demeter. So what's the deal?