Skip to content

Commit

Permalink
resolving (BAEL-5148) Get String Character by Index in Java (eugenp#1…
Browse files Browse the repository at this point in the history
…1340)

* 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
  • Loading branch information
hajk1 committed Oct 23, 2021
1 parent 2915948 commit 603a1f3
Showing 1 changed file with 29 additions and 0 deletions.
@@ -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)));
}

}

0 comments on commit 603a1f3

Please sign in to comment.