From 78d0fa3656aec1f42290eb23ac8f31ffe902b13a Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sat, 7 Oct 2023 22:47:58 +0530 Subject: [PATCH] Updated Junits --- .../java/com/baeldung/dto/BooleanObject.java | 1 + .../ValidationControllerUnitTest.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java index 410324d3b5..750b23fe11 100644 --- a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java @@ -15,6 +15,7 @@ public class BooleanObject { @AssertTrue(message = "trueField must have true value") Boolean trueField; + @NotNull(message = "falseField cannot be null") @AssertFalse(message = "falseField must have false value") Boolean falseField; diff --git a/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java index 759007ed36..ad0e3ecdfa 100644 --- a/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java +++ b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java @@ -109,4 +109,30 @@ class ValidationControllerUnitTest { mockMvc.perform(post("/validateBooleanAtService").contentType("application/json")) .andExpect(status().isInternalServerError()); } + + @Test + void whenNullInputForTrueBooleanField_thenCorrectResponse() throws Exception { + + String postBody = "{\"boolField\":true,\"trueField\":null,\"falseField\":false,\"boolStringVar\":\"+\"}"; + + mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andExpect(status().isOk()); + + } + + @Test + void whenNullInputForFalseBooleanField_thenHttpBadRequestAsHttpResponse() throws Exception { + + String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":null,\"boolStringVar\":\"+\"}"; + + String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andReturn() + .getResponse() + .getContentAsString(); + + assertEquals("falseField cannot be null", output); + + } }