resolving (BAEL-5148) Get String Character by Index in Java (#11340)

* resolving (BAEL-5148) Get String Character by Index in Java

* changing method name from *_thenCorrect() to *_thenSuccess()

* reverted README changes according to the editor suggestion

* extra snippet removed inorder to comply with the article edits
This commit is contained in:
Kayvan Tehrani 2021-10-23 20:16:51 +03:30 committed by GitHub
parent 9f1155140d
commit d6be7a2528
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.stringapi;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class StringCharAtUnitTest {
@Test
public void whenCallCharAt_thenSuccess() {
String sample = "abcdefg";
assertEquals('d', sample.charAt(3));
}
@Test()
public void whenCharAtNonExist_thenIndexOutOfBoundsExceptionThrown() {
String sample = "abcdefg";
assertThrows(IndexOutOfBoundsException.class, () -> sample.charAt(-1));
assertThrows(IndexOutOfBoundsException.class, () -> sample.charAt(sample.length()));
}
@Test
public void whenCallCharAt_thenReturnString() {
String sample = "abcdefg";
assertEquals("a", Character.toString(sample.charAt(0)));
assertEquals("a", String.valueOf(sample.charAt(0)));
}
}