Merge pull request #6907 from rockoder/java12-string-api

Java 12 String API Test
This commit is contained in:
Eric Martin 2019-05-16 22:50:29 -05:00 committed by GitHub
commit d43790d722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package com.baeldung.string;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
public class StringAPITest {
@Test
public void whenPositiveArgument_thenReturnIndentedString() {
String multilineStr = "This is\na multiline\nstring.";
String outputStr = " This is\n a multiline\n string.\n";
String postIndent = multilineStr.indent(3);
assertThat(postIndent, equalTo(outputStr));
}
@Test
public void whenNegativeArgument_thenReturnReducedIndentedString() {
String multilineStr = " This is\n a multiline\n string.";
String outputStr = " This is\n a multiline\n string.\n";
String postIndent = multilineStr.indent(-2);
assertThat(postIndent, equalTo(outputStr));
}
@Test
public void whenTransformUsingLamda_thenReturnTransformedString() {
String result = "hello".transform(input -> input + " world!");
assertThat(result, equalTo("hello world!"));
}
@Test
public void whenTransformUsingParseInt_thenReturnInt() {
int result = "42".transform(Integer::parseInt);
assertThat(result, equalTo(42));
}
}