I'm new in unit testing. For last two days I'm thinking how to test non-atomic methods with unit tests. When i need to write tests for some simple, atomic method situation in pretty easy - i'm taking all scenarios for this method that i can imagine, and reforge each of it in unit test.
But what if i have methods that are calling other methods? Should I test each scenarios for tested method AND each methods it is calling?
Maybe some example will be easier to understand:
public int makeSomeOperation(int a, int b)
{
if (a < 0)
throw new ArgumentException("a must be greater that 0");
if (b == 0)
throw new ArgumentException("b cannot be 0");
return a/b;
}
public bool SomeNonatomicMethod(int number)
{
if (number * number % 2 == 1)
return false;
int secondNumber = getNumberFromMordor();
int result = makeSomeOperation(number, secondNumber);
if (result == "123")
return true;
return false;
}
In this case, makeSomeOperation
is easy to test - one test for each exception, one to check in returning result is equals with expectations.
But what about SomeNonatomicMethod? Did I need to write not only test for each case in this method (so: power of number is odd, result is equal 123, and result is diffrent), but also each possible flow of things that may happen in getNumberFromMordor
and makeSomeOperation
?
Things that may happen in makeSomeOperation
are already tested, so are they should be tested second time?