From 611afab48f28ac5e2e269059a06806caad8296c2 Mon Sep 17 00:00:00 2001 From: Sourabh Bhat Date: Thu, 17 Aug 2023 13:26:10 +0200 Subject: [PATCH] return optional ofNullable, as the boundary condition may be undefined during initialization. --- src/main/mesh/Boundary.java | 6 +++--- test/main/mesh/BoundaryTest.java | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/main/mesh/BoundaryTest.java diff --git a/src/main/mesh/Boundary.java b/src/main/mesh/Boundary.java index 5fea216..b85b8d8 100644 --- a/src/main/mesh/Boundary.java +++ b/src/main/mesh/Boundary.java @@ -12,8 +12,8 @@ public class Boundary { private BoundaryCondition bc; public Boundary(String name, List faces, BoundaryCondition bc) { - this.name = Objects.requireNonNull(name); - this.faces = Objects.requireNonNull(faces); + this.name = Objects.requireNonNull(name, "Boundary 'name' cannot be null."); + this.faces = Objects.requireNonNull(faces, "Boundary 'faces' cannot be null."); this.bc = bc; } @@ -22,6 +22,6 @@ public void setBC(BoundaryCondition bc) { } public Optional bc() { - return Optional.of(bc); + return Optional.ofNullable(bc); } } diff --git a/test/main/mesh/BoundaryTest.java b/test/main/mesh/BoundaryTest.java new file mode 100644 index 0000000..3c3c3a9 --- /dev/null +++ b/test/main/mesh/BoundaryTest.java @@ -0,0 +1,31 @@ +package main.mesh; + +import org.junit.Test; + +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.*; + +public class BoundaryTest { + + @Test + public void constructor_nullability_test() { + // null name + assertThrows(NullPointerException.class, () -> new Boundary(null, List.of(), null)); + // null faces + assertThrows(NullPointerException.class, () -> new Boundary("name", null, null)); + } + + @Test + public void bc_optionality_test() { + Boundary bnd = new Boundary("bnd name", List.of(), null); + assertEquals(Optional.empty(), bnd.bc()); + } + + @Test + public void trying_to_set_null_bc() { + Boundary bnd = new Boundary("bnd name", List.of(), null); + assertThrows(NullPointerException.class, () -> bnd.setBC(null)); + } +} \ No newline at end of file