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
This commit is contained in:
parent
ec02aed456
commit
dd50f5e80b
|
@ -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<String> getGreetingWithResponseEntity() {
|
||||||
|
final HttpHeaders httpHeaders= new HttpHeaders();
|
||||||
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
return new ResponseEntity<String>("{\"test\": \"Hello with ResponseEntity\"}", httpHeaders, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
@RequestMapping(
|
||||||
|
value = "/greetings-with-map-return-type",
|
||||||
|
method = RequestMethod.GET,
|
||||||
|
produces = "application/json"
|
||||||
|
)
|
||||||
|
@ResponseBody
|
||||||
|
public Map<String, Object> getGreetingWhileReturnTypeIsMap() {
|
||||||
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
||||||
|
map.put("test", "Hello from map");
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue