last n characters from string unit tests

This commit is contained in:
danielmcnally285 2024-03-16 12:23:28 +00:00
parent c30d36118d
commit 82ca2b8b92
1 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package com.baeldung.lastncharacters;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class LastNCharactersUnitTest {
private String s;
private int n;
@Test
void givenString_whenUsingIntStreamAsStreamSource_thenObtainLastNCharacters() {
String result = s.chars()
.mapToObj(c -> (char) c)
.skip(s.length() - n)
.map(String::valueOf)
.collect(Collectors.joining());
assertThat(result).isEqualTo("2024");
}
@Test
void givenString_whenUsingOneArgSubstringMethod_thenObtainLastNCharacters() {
int beginIndex = s.length() - n;
assertThat(s.substring(beginIndex)).isEqualTo("2024");
}
@Test
void givenString_whenUsingStreamOfCharactersAsSource_thenObtainLastNCharacters() {
String result = Arrays.stream(ArrayUtils.toObject(s.toCharArray()))
.skip(s.length() - n)
.map(String::valueOf)
.collect(Collectors.joining());
assertThat(result).isEqualTo("2024");
}
@Test
void givenString_whenUsingStringUtilsRight_thenObtainLastNCharacters() {
assertThat(StringUtils.right(s, n)).isEqualTo("2024");
}
@Test
void givenString_whenUsingTwoArgSubstringMethod_thenObtainLastNCharacters() {
int beginIndex = s.length() - n;
String result = s.substring(beginIndex, s.length());
assertThat(result).isEqualTo("2024");
}
@BeforeEach
void init() {
s = "10-03-2024";
n = 4;
}
}