Skip to content

Commit b29cb58

Browse files
iaforekpmaric
authored andcommitted
BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests.
(cherry picked from commit 124966a)
1 parent 6e58963 commit b29cb58

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎core-java/src/main/java/com/baeldung/string/RemoveLastChar.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class RemoveLastChar {
1111
public static String substring (String s) {
1212
if (s == null || s.length() == 0) {
13-
return null;
13+
return s;
1414
} else {
1515
return (s.substring(0, s.length() - 1));
1616
}

‎core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class RemoveLastCharTest {
2626
public void givenTestString_substring_getStingWithoutLastChar() {
2727
Assert.assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
2828
Assert.assertEquals("abcde", StringUtils.chop(TEST_STRING));
29+
Assert.assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
2930
}
3031

3132
@Test
@@ -37,20 +38,25 @@ public void givenNullString_substring_getNullString() {
3738

3839
@Test
3940
public void givenEmptyString_substring_getEmptyString() {
40-
Assert.assertEquals(NULL_STRING, RemoveLastChar.substring(EMPTY_STRING));
41-
Assert.assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
41+
Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
42+
Assert.assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
43+
Assert.assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
44+
4245
}
4346

4447
@Test
4548
public void givenOneCharString_substring_getEmptyString() {
4649
Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
4750
Assert.assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
51+
Assert.assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
52+
4853
}
4954

5055
@Test
5156
public void givenStringWithWhiteSpaceAtTheEnd_substring_getStringWithoutWhiteSpaceAtTheEnd() {
5257
Assert.assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
5358
Assert.assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
59+
Assert.assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
5460
}
5561

5662
}

0 commit comments

Comments
 (0)