diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/printnullvalues/Employee.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/printnullvalues/Employee.java new file mode 100644 index 0000000000..b4cd62897b --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/printnullvalues/Employee.java @@ -0,0 +1,44 @@ +package com.baeldung.printnullvalues; + +import java.util.Objects; +import java.util.Optional; + +public class Employee { + private String name; + private int age; + private String department; + + public Employee(String name, int age, String department) { + this.name = name; + this.age = age; + this.department = department; + } + + public String toStringUsingNullCheck() { + return "Name: " + (name != null ? name : "Unknown") + + ", Age: " + age + + ", Department: " + (department != null ? department : "Unknown"); + } + + public String toStringUsingOptional() { + return "Name: " + Optional.ofNullable(name).orElse("Unknown") + + ", Age: " + age + + ", Department: " + Optional.ofNullable(department).orElse("Unknown"); + } + + private String getDefaultIfNull(String value, String defaultValue) { + return value != null ? value : defaultValue; + } + + public String toStringUsingCustomHelper() { + return "Name: " + getDefaultIfNull(name, "Unknown") + + ", Age: " + age + + ", Department: " + getDefaultIfNull(department, "Unknown"); + } + + public String toStringUsingObjects() { + return "Name: " + Objects.toString(name, "Unknown") + + ", Age: " + age + + ", Department: " + Objects.toString(department, "Unknown"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/printnullvalues/PrintingNullValuesUnitTest.java b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/printnullvalues/PrintingNullValuesUnitTest.java new file mode 100644 index 0000000000..7526cd9ac4 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/printnullvalues/PrintingNullValuesUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.printnullvalues; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PrintingNullValuesUnitTest { + Employee employee = new Employee(null, 30, null); + String expected = "Name: Unknown, Age: 30, Department: Unknown"; + + @Test + public void givenNullValues_whenToStringUsingNullCheck_thenCorrectStringReturned() { + assertEquals(expected, employee.toStringUsingNullCheck()); + } + + @Test + public void givenNullValues_whenToStringUsingOptional_thenCorrectStringReturned() { + assertEquals(expected, employee.toStringUsingOptional()); + } + + @Test + public void givenNullValues_whenToStringUsingCustomHelper_thenCorrectStringReturned() { + assertEquals(expected, employee.toStringUsingCustomHelper()); + } + + @Test + public void givenNullValues_whenToStringUsingObjects_thenCorrectStringReturned() { + assertEquals(expected, employee.toStringUsingObjects()); + } +}