Sunday, December 02, 2007

Unit Testing void Methods - Part 2

Dissection of void Methods with parameters

Methods with void return types incur complexities in writing unit tests. So, we need to characterize the void methods to make sure we have guards against the odds for testability.
For example, the following is a void method with parameters

public void DoSomething(SomeType obj, SomeOtherType objOther)

{

//1.modifies some member variables

this.someMemberVariable = 10;

//2.Calls methods of the same class or other classes

someOtherClass.SomeOtherMethod(someArg);

//3.Sometimes throws exception


//4.Modifies call by reference type argument objects

obj.SomeProperty = objOther.GetResultOfSomeAction();

objOther.DeleteSomething();

}


Unit test examples for all the scenarios other than 4 are discussed in Part -1 of this article.

d. In such cases, the unit test assertions are made to test against the expected value of the supplied argument objects after the method is invoked.