From dd50f5e80bdb0862bf88f528356f3c50c8db71f4 Mon Sep 17 00:00:00 2001 From: Usman Mohyuddin Date: Sun, 25 Jul 2021 22:02:16 +0500 Subject: [PATCH] Create GreetingsController.java (#11057) * Create GreetingsController.java add controller to test the content type being set properly or not * add unit tests for greetings controller * update to avoid PMD violation * correct the file type * Update GreetingsControllerUnitTest.java fix some imports & correct the names * Update GreetingsControllerUnitTest.java update the unit case to avoid internal server error for junit --- .../controller/GreetingsController.java | 49 ++++++++++++++++ .../GreetingsControllerUnitTest.java | 58 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 spring-web-modules/spring-mvc-basics-4/src/main/java/com/baeldung/controller/controller/GreetingsController.java create mode 100644 spring-web-modules/spring-mvc-basics-4/src/test/java/com/baeldung/controller/GreetingsControllerUnitTest.java diff --git a/spring-web-modules/spring-mvc-basics-4/src/main/java/com/baeldung/controller/controller/GreetingsController.java b/spring-web-modules/spring-mvc-basics-4/src/main/java/com/baeldung/controller/controller/GreetingsController.java new file mode 100644 index 0000000000..fbf78b8a0e --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-4/src/main/java/com/baeldung/controller/controller/GreetingsController.java @@ -0,0 +1,49 @@ +package com.baeldung.controller.controller; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.http.HttpHeaders; +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.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class GreetingsController { + + @RequestMapping( + value = "/greetings-with-response-body", + method = RequestMethod.GET, + produces="application/json" + ) + @ResponseBody + public String getGreetingWhileReturnTypeIsString() { + return "{\"test\": \"Hello using @ResponseBody\"}"; + } + + @RequestMapping( + value = "/greetings-with-response-entity", + method = RequestMethod.GET, + produces = "application/json" + ) + public ResponseEntity getGreetingWithResponseEntity() { + final HttpHeaders httpHeaders= new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + return new ResponseEntity("{\"test\": \"Hello with ResponseEntity\"}", httpHeaders, HttpStatus.OK); + } + @RequestMapping( + value = "/greetings-with-map-return-type", + method = RequestMethod.GET, + produces = "application/json" + ) + @ResponseBody + public Map getGreetingWhileReturnTypeIsMap() { + HashMap map = new HashMap(); + map.put("test", "Hello from map"); + return map; + } +} \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-basics-4/src/test/java/com/baeldung/controller/GreetingsControllerUnitTest.java b/spring-web-modules/spring-mvc-basics-4/src/test/java/com/baeldung/controller/GreetingsControllerUnitTest.java new file mode 100644 index 0000000000..ee9a8da8d4 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-4/src/test/java/com/baeldung/controller/GreetingsControllerUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.baeldung.controller.controller.GreetingsController; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.AnnotationConfigWebContextLoader; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { GreetingsController.class }, loader = AnnotationConfigWebContextLoader.class) +public class GreetingsControllerUnitTest { + + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext wac; + + @Before + public void setUp() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + } + + @Test + public void givenReturnTypeIsString_whenJacksonOnClasspath_thenDefaultContentTypeIsJSON() throws Exception { + + // Given + String expectedMimeType = "application/json"; + + // Then + String actualMimeType = this.mockMvc.perform(MockMvcRequestBuilders.get("/greetings-with-response-body", 1)).andReturn().getResponse().getContentType(); + + Assert.assertEquals(expectedMimeType, actualMimeType); + + } + + @Test + public void givenReturnTypeIsResponseEntity_thenDefaultContentTypeIsJSON() throws Exception { + + // Given + String expectedMimeType = "application/json"; + + // Then + String actualMimeType = this.mockMvc.perform(MockMvcRequestBuilders.get("/greetings-with-response-entity", 1)).andReturn().getResponse().getContentType(); + + Assert.assertEquals(expectedMimeType, actualMimeType); + } +} \ No newline at end of file