Issue
I am writing Unit tests for my controller methods and many of the methods call on other methods which then also calls on other methods until finally returning a result. Now from what I have read on best practices one should test each method even if it has been essentially "tested" through another method.
To put this in perspective lets say I have these 3 methods and I do an assertTrue(MethodA) is it really necessary that I test MethodB and then MethodC as well? If so why?
public Boolean MethodA(Object o)
{
try {
//Do stuff
return MethodB(Object o);
}
catch(Exception e)
{
return false;
}
}
public Boolean MethodB(Object o)
{
try {
//Do stuff
return MethodC(Object o);
}
catch(Exception e)
{
return false;
}
}
public Boolean MethodC(Object o)
{
try {
//Do stuff with o
//return True
}
catch(Exception e)
{
return false;
}
}
Solution
It depends on you and your team actually. In this case I would probably test them all.
One reason being: how else could I ensure that when methodA()
returned false
that it is due to the calculation that was done in methodB()
or an exception from there? Same question applies to methodC()
as well... if there is thrown an exception we get false
, but how should I know whether (actually: ensure that) it was due to an exception or from an actual calculation?
If you would test that through methodA()
only that test alone will probably become way too complex and you would add too much logic of the inner workings to that specific test(s). Try to test/challenge your method from an outside view. Everything that is visible from outside you probably want to test alone. Also: keep your tests simple enough that when you return you can easily grasp why you tested it this way.
Answered By - Roland