diff --git a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java index b6cd8ba..487ad47 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java @@ -15,6 +15,8 @@ */ package org.springframework.samples.petclinic.model; +import java.util.Objects; + import javax.annotation.Nullable; import javax.persistence.Column; import javax.persistence.MappedSuperclass; @@ -44,7 +46,7 @@ public void setName(@Nullable String name) { @Override public String toString() { - return this.getName(); + return Objects.toString(getName()); } } diff --git a/src/test/java/org/springframework/samples/petclinic/model/NamedEntityTests.java b/src/test/java/org/springframework/samples/petclinic/model/NamedEntityTests.java new file mode 100644 index 0000000..df31113 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/model/NamedEntityTests.java @@ -0,0 +1,12 @@ +package org.springframework.samples.petclinic.model; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class NamedEntityTests { + @Test + public void toStringReturnsNonNullString() { + assertThat(new Pet().toString()).isEqualTo("null"); + } +}