1

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 !

3
  • You're not combining the results in the code you've shown. Commented May 21, 2010 at 20:59
  • 2
    I have used the result of someOperationOne() as an argument for someOtherOperation() Commented May 21, 2010 at 21:04
  • Oh, sorry, I didn't catch that. Commented May 21, 2010 at 21:06

2 Answers 2

5

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".

Sign up to request clarification or add additional context in comments.

Comments

2

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().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.