what is the appropriate way for writing JunitTestCases for this method

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 ??

Answers


This is question of style. These are the two alternatives: With all in one method:

public void testAll() {
    assertEquals("ATE", getData(leg4));
    assertEquals("FTE", getData(leg2));
    // etc.
}

With one test method / combination:

public void testLength4() {
    assertEquals("ATE", getData(leg4));
}

public void testLength2() {
    assertEquals("FTE", getData(leg2));
}

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.


Need Your Help

android: create a new activity with xml page

java android xml android-activity

I want to create a new activity. My default has two files MainActivity.xml with MainActivity.java. But, If i created a new activity only .java files are created in /src/ path no .xml files are crea...

Display image size in MB format IOS 7

ios iphone image-size

I tying to check my image size after picking form the photo library by using this code.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.