I'm testing a java method by Junit. The method takes a String then returns a modify String. I've already written a common test, a test for Null and Empty String. What tests can I write yet? What is boundary conditions for that test? What are large and small String for the test of that method?
public String reverse(String text) {
String[] textFragments = text.split(" ");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < textFragments.length; i++) {
char[] chars = textFragments[i].toCharArray();
int leftElementIndex = 0;
int rightElementIndex = textFragments[i].length() - 1;
while (leftElementIndex < rightElementIndex) {
boolean isLeftLetter = Character.isLetter(chars[leftElementIndex]);
boolean isRightLetter = Character.isLetter(chars[rightElementIndex]);
if (isLeftLetter && isRightLetter) {
swap(chars, leftElementIndex, rightElementIndex);
leftElementIndex++;
rightElementIndex--;
} else {
leftElementIndex = (!isLeftLetter) ? ++leftElementIndex : leftElementIndex;
rightElementIndex = (!isRightLetter) ? --rightElementIndex : rightElementIndex;
}
}
stringBuilder.append(new String(chars));
if (i != (textFragments.length - 1)) {
stringBuilder.append(" ");
}
}
return stringBuilder.toString();
}
Boundary conditions are derived from requirements, so it all depends of how you define the expected input string. So if "text" is limited have a length of 10, non empty, space separated, and lower case only, then these group becomes part of its boundaries. Based on the code provided, it could be defined as a not null, non empty, at least a character long string, and since your loop depends on the array size then try to trick the split method by passing strings with more than one space in between characters or a string only composed of spaces.