public static void main(String[ ] args) { test(stringLength(null), 0, "length of null"); test(stringLength(""), 0, "length of empty string"); test(stringLength("AAA"), 3, "length of AAA"); } code example
Example: public static void main(String[ ] args) { test(stringLength(null), 0, "length of null"); test(stringLength(""), 0, "length of empty string"); test(stringLength("AAA"), 3, "length of AAA"); }
class Playground {
private static char getLastCharacter(String input) {
return 'a';
}
public static void main(String[ ] args) {
test(getLastCharacter("a"), 'a', "\"a\" last character");
test(getLastCharacter("abcde"), 'e', "abcde last character");
test(getLastCharacter("12345"), '5', "12345 last character");
}
private static void test(char actual, char expected, String testName) {
if (actual != expected) {
String errorMessage = String.format(
"Test %s failed: %s is not equal to expected %s",
testName,
actual,
expected);
System.out.println("ERROR: " + errorMessage);
} else {
System.out.println("SUCCESS: " + testName + " passed");
}
}
}