From 1397143b46b13efdaf02b3eaf229d62e6c642fa4 Mon Sep 17 00:00:00 2001 From: Justin Albano Date: Tue, 10 Mar 2020 14:50:45 -0400 Subject: [PATCH 1/3] BAEL-3871: Added tests for duplicate HTTP controller methods with different response types. --- .../FooMappingExamplesController.java | 18 +++++----- .../FooMappingExamplesControllerTest.java | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerTest.java diff --git a/spring-rest-http/src/main/java/com/baeldung/requestmapping/FooMappingExamplesController.java b/spring-rest-http/src/main/java/com/baeldung/requestmapping/FooMappingExamplesController.java index 90012ed05411..543ba76273b3 100644 --- a/spring-rest-http/src/main/java/com/baeldung/requestmapping/FooMappingExamplesController.java +++ b/spring-rest-http/src/main/java/com/baeldung/requestmapping/FooMappingExamplesController.java @@ -1,6 +1,8 @@ package com.baeldung.requestmapping; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -103,8 +105,8 @@ public String putAndPostFoos() { // --- Ambiguous Mapping @GetMapping(value = "foos/duplicate" ) - public String duplicate() { - return "Duplicate"; + public ResponseEntity duplicate() { + return new ResponseEntity<>("Duplicate", HttpStatus.OK); } // uncomment for exception of type java.lang.IllegalStateException: Ambiguous mapping @@ -114,14 +116,14 @@ public String duplicate() { // return "Duplicate"; // } - @GetMapping(value = "foos/duplicate/xml", produces = MediaType.APPLICATION_XML_VALUE) - public String duplicateXml() { - return "Duplicate Xml"; + @GetMapping(value = "foos/duplicate", produces = MediaType.APPLICATION_XML_VALUE) + public ResponseEntity duplicateXml() { + return new ResponseEntity<>("Duplicate", HttpStatus.OK); } - @GetMapping(value = "foos/duplicate/json", produces = MediaType.APPLICATION_JSON_VALUE) - public String duplicateJson() { - return "Duplicate Json"; + @GetMapping(value = "foos/duplicate", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity duplicateJson() { + return new ResponseEntity<>("{\"message\":\"Duplicate\"}", HttpStatus.OK); } } \ No newline at end of file diff --git a/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerTest.java b/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerTest.java new file mode 100644 index 000000000000..d8e54cc1840b --- /dev/null +++ b/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerTest.java @@ -0,0 +1,35 @@ +package com.baeldung.requestmapping; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@RunWith(SpringRunner.class) +@WebMvcTest(FooMappingExamplesController.class) +public class FooMappingExamplesControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + public void givenAcceptsJson_whenGetDuplicate_thenJsonResponseReturned() throws Exception { + mvc.perform(get("/ex/foos/duplicate") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("{\"message\":\"Duplicate\"}")); + } + + @Test + public void givenAcceptsXml_whenGetDuplicate_thenXmlResponseReturned() throws Exception { + mvc.perform(get("/ex/foos/duplicate") + .accept(MediaType.APPLICATION_XML)) + .andExpect(status().isOk()) + .andExpect(content().string("Duplicate")); + } +} From 7458950e5697183446f28409260ae417f85fd859 Mon Sep 17 00:00:00 2001 From: Justin Albano Date: Thu, 12 Mar 2020 09:05:59 -0400 Subject: [PATCH 2/3] BAEL-3871: Added "Unit" suffix to test to follow naming convention --- ...trollerTest.java => FooMappingExamplesControllerUnitTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spring-rest-http/src/test/java/com/baeldung/requestmapping/{FooMappingExamplesControllerTest.java => FooMappingExamplesControllerUnitTest.java} (100%) diff --git a/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerTest.java b/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerUnitTest.java similarity index 100% rename from spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerTest.java rename to spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerUnitTest.java From c87f03226060aad4e24cd16a7f0cbbbe728a66e0 Mon Sep 17 00:00:00 2001 From: Justin Albano Date: Sat, 14 Mar 2020 18:09:00 -0400 Subject: [PATCH 3/3] BAEL-3871: Corrected name of test to include "UnitTest" --- .../requestmapping/FooMappingExamplesControllerUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerUnitTest.java b/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerUnitTest.java index d8e54cc1840b..388b4b735475 100644 --- a/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerUnitTest.java +++ b/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerUnitTest.java @@ -12,7 +12,7 @@ @RunWith(SpringRunner.class) @WebMvcTest(FooMappingExamplesController.class) -public class FooMappingExamplesControllerTest { +public class FooMappingExamplesControllerUnitTest { @Autowired private MockMvc mvc;