BAEL-7057: How to convert CharSequence to String? (#14892)

This commit is contained in:
Azhwani 2023-10-09 09:55:29 +02:00 committed by GitHub
parent 901951bff7
commit 52f4344bce
1 changed files with 41 additions and 2 deletions

View File

@ -1,11 +1,11 @@
package com.baeldung.charsequence;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class CharSequenceVsStringUnitTest {
@Test
@ -44,4 +44,43 @@ public class CharSequenceVsStringUnitTest {
assertEquals(firstAddressOfTest, secondAddressOfTest);
}
@Test
public void givenCharSequenceAsString_whenConvertingUsingCasting_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence = "baeldung";
String explicitCastedString = (String) charSequence;
assertEquals(expected, charSequence);
assertEquals(expected, explicitCastedString);
}
@Test(expected = ClassCastException.class)
public void givenCharSequenceAsStringBuiler_whenConvertingUsingCasting_thenThrowException() {
CharSequence charSequence = new StringBuilder("baeldung");
String castedString = (String) charSequence;
}
@Test
public void givenCharSequence_whenConvertingUsingToString_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence1 = "baeldung";
CharSequence charSequence2 = new StringBuilder("baeldung");
assertEquals(expected, charSequence1.toString());
assertEquals(expected, charSequence2.toString());
}
@Test
public void givenCharSequence_whenConvertingUsingValueOf_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence1 = "baeldung";
CharSequence charSequence2 = new StringBuilder("baeldung");
CharSequence charSequence3 = null;
assertEquals(expected, String.valueOf(charSequence1));
assertEquals(expected, String.valueOf(charSequence2));
assertEquals("null", String.valueOf(charSequence3));
}
}