From bbdf2ae68606b19dedd331453375fadc9dbc315c Mon Sep 17 00:00:00 2001 From: "mateusz.szablak" Date: Thu, 15 Oct 2020 16:08:49 +0200 Subject: [PATCH] BAEL-4212 (String) or .toString()? - code + tests --- .../baeldung/tostring/StringCastUtils.java | 10 ++ .../baeldung/tostring/ToStringUnitTest.java | 92 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java create mode 100644 core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java diff --git a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java new file mode 100644 index 0000000000..056f961ea4 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java @@ -0,0 +1,10 @@ +package com.baeldung.tostring; + +public class StringCastUtils { + public static String castToString(Object object) { + if (object instanceof String) { + return (String) object; + } + return null; + } +} diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java new file mode 100644 index 0000000000..88202e1797 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.tostring; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ToStringUnitTest { + @Test + public void givenString_whenCastToObjectAndString_thenSameAndNoException() { + String input = "baeldung"; + + Object obj = input; + String str = (String) obj; + + assertEquals(obj, str); + assertEquals(str, input); + assertSame(input, str); + } + + @Test(expected = ClassCastException.class) + public void givenIntegerObject_whenCastToObjectAndString_thenCastClassException() { + Integer input = 1234; + + Object obj = input; + String str = (String) obj; + } + + @Test + public void givenNullInteger_whenCastToObjectAndString_thenSameAndNoException() { + Integer input = null; + + Object obj = input; + String str = (String) obj; + + assertEquals(obj, str); + assertEquals(str, input); + assertSame(input, str); + } + + @Test(expected = NullPointerException.class) + public void givenNullInteger_whenToString_thenNullPointerException() { + Integer input = null; + + String str = input.toString(); + } + + @Test + public void givenInteger_whenCastToObject_thenToStringEquals() { + Integer input = 1234; + + Object obj = input; + + assertEquals("1234", input.toString()); + assertEquals("1234", obj.toString()); + assertEquals(input.toString(), obj.toString()); + } + + @Test + public void givenString_whenToString_thenSame() { + String str = "baeldung"; + + assertEquals("baeldung", str.toString()); + assertSame(str, str.toString()); + } + + @Test + public void givenString_whenCastToObject_thenCastToStringReturnsSame() { + String input = "baeldung"; + + Object obj = input; + + assertSame(input, StringCastUtils.castToString(obj)); + } + + @Test + public void givenInteger_whenCastToObject_thenCastToStringReturnsNull() { + Integer input = 1234; + + Object obj = input; + + assertEquals(null, StringCastUtils.castToString(obj)); + } + + @Test + public void givenIntegerNull_whenCastToObject_thenCastToStringReturnsNull() { + Integer input = null; + + Object obj = input; + + assertEquals(null, StringCastUtils.castToString(obj)); + } +}