Merge pull request #621 from eugenp/grzegorz_char_to_string

Add code examples for Char to String mini-article
This commit is contained in:
Alex Theedom 2016-08-22 18:27:25 +01:00 committed by GitHub
commit ecacc8d50a

View File

@ -0,0 +1,53 @@
package com.baeldung;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class CharToStringTest {
@Test
public void givenChar_shouldConvertToString1() throws Exception {
final char givenChar = 'x';
final String result = String.valueOf(givenChar);
assertThat(result).isEqualTo("x");
}
@Test
public void givenChar_shouldConvertToString2() throws Exception {
final char givenChar = 'x';
final String result = Character.toString(givenChar);
assertThat(result).isEqualTo("x");
}
@Test
public void givenChar_shouldConvertToString3() throws Exception {
final char givenChar = 'x';
final String result = new Character(givenChar).toString();
assertThat(result).isEqualTo("x");
}
@Test
public void givenChar_shouldConvertToString4() throws Exception {
final char givenChar = 'x';
final String result = givenChar + "";
assertThat(result).isEqualTo("x");
}
@Test
public void givenChar_shouldConvertToString5() throws Exception {
final char givenChar = 'x';
final String result = String.format("%c", givenChar);
assertThat(result).isEqualTo("x");
}
}