The below code is my Method logic .
pulic String getData()
{
if (leg.length == 4)
return "ATE";
else if (leg.length == 2) {
return "FTE";
}
if (leg.length == 3) {
if (xr1.KotilaType.equals("CST")) {
return "HTE";
}
return "DTE";
}
return "BTE";
}
As ordered to me, i need to start writing Junit Test Cases for this Method.
As you can see there is only One method and a number of conditions inside this method .
Now my question is ,
Do we need to write a Seperate Junit TestCase ( A Seperate Method) for each condition ??
Or will it be sufficient to write only One Junit TestCase and cover all the above conditions ??
I am new to junit and not a expert , please guide me what is the approiate way for writing JunitTestCases for the above method ??
This is question of style. These are the two alternatives: With all in one method:
With one test method / combination:
The former has the advantage that it is very easy to see all combinations, because they’re all in one place. This is very useful if there are a lot of combinations or the combinations are complex.
The latter has the advantage that you have one test per combination, so it’s very easy to localize a problem, because (theoretically) only one test will fail. Disadvantage: you very quickly run out of names for test methods. testLength4, testLength2, testLength3WithCST, testLength3WithoutCST. This disadvantage gets particularly bad when you have a lot of combinations (say like one test for each month in a year).
Most of the time, I use one test method / test case, but if the number of combinations starts to get high, I would have a single method. This contains all of the combinations, because I like to be able to see all of my combinations at once, to see if I’ve missed any.