views:

32

answers:

2

Hi,

This is my doubt on what we regard as a "unit" while unit-testing.

say I have a method like this,

public String myBigMethod()
      {
         String resultOne = moduleOneObject.someOperation(); 
         String resultTwo = moduleTwoObject.someOtherOperation(resultOne);
         return resultTwo;
      }

( I have unit-tests written for someOperation() and someOtherOperation() seperately )

and this myBigMethod() kinda integrates ModuleOne and ModuleTwo by using them as above,

then, is the method "myBigMethod()" still considered as a "unit" ?

Should I be writing a test for this "myBigMethod()" ?

Say I have written a test for myBigMethod()... If testSomeOperation() fails, it would also result in testMyBigMethod() to fail... Now testMyBigMethod()'s failure might show a not-so-correct-location of the bug.

One-Cause causing two tests to fail doesn't look so good to me. But donno if there's any better way...? Is there ?

Thanks !

+4  A: 

You want to test the logic of myBigMethod without testing the dependencies.

It looks like the specification of myBigMethod is:

  1. Call moduleOneObject.someOperation
  2. Pass the result into moduleTwoObject.someOtherOperation
  3. Return the result

The key to testing just this behavior is to break the dependencies on moduleOneObject and moduleTwoObject. Typically this is done by passing the dependencies into the class under test in the constructor (constructor injection) or setting them via properties (setter injection).

The question isn't just academic because in practice moduleOneObject and moduleTwoObject could go out and hit external systems such as a database. A true unit test doesn't hit external systems as that would make it an "integration test".

Rob
+2  A: 

The test for myBigMethod() should test the combination of the results of the other two methods called. So, yes it should fail if either of the methods it depends on fails, but it should be testing more. There should be some case where someOperation() and someOtherOperation() work correctly, but myBigMethod() can still fail. If that's not possible, then there's no need to test myBigMethod().

Bill the Lizard
thanks ! that clarifies it for me..! :)
stratwine