Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Spring's @RequestBody using Spring MockMVC

I am trying to test a method that posts an object to the database using Spring's MockMVC framework. I've constructed the test as follows:

@Test public void testInsertObject() throws Exception {       String url = BASE_URL + "/object";      ObjectBean anObject = new ObjectBean();     anObject.setObjectId("33");     anObject.setUserId("4268321");     //... more      Gson gson = new Gson();     String json = gson.toJson(anObject);      MvcResult result = this.mockMvc.perform(             post(url)             .contentType(MediaType.APPLICATION_JSON)             .content(json))             .andExpect(status().isOk())             .andReturn(); } 

The method I'm testing uses Spring's @RequestBody to receive the ObjectBean, but the test always returns a 400 error.

@ResponseBody @RequestMapping(    consumes="application/json",                     produces="application/json",                     method=RequestMethod.POST,                     value="/object") public ObjectResponse insertObject(@RequestBody ObjectBean bean){      this.photonetService.insertObject(bean);      ObjectResponse response = new ObjectResponse();     response.setObject(bean);      return response; } 

The json created by gson in the test:

{    "objectId":"33",    "userId":"4268321",    //... many more } 

The ObjectBean class

public class ObjectBean {  private String objectId; private String userId; //... many more  public String getObjectId() {     return objectId; }  public void setObjectId(String objectId) {     this.objectId = objectId; }  public String getUserId() {     return userId; }  public void setUserId(String userId) {     this.userId = userId; } //... many more } 

So my question is: how to I test this method using Spring MockMVC?

like image 464
Matt Avatar asked Dec 10 '13 20:12

Matt


People also ask

What is MockMvc in spring?

MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container. In this MockMVC tutorial, we will use it along with Spring boot's WebMvcTest class to execute Junit testcases which tests REST controller methods written for Spring boot 2 hateoas example.


1 Answers

Use this one

public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));  @Test public void testInsertObject() throws Exception {      String url = BASE_URL + "/object";     ObjectBean anObject = new ObjectBean();     anObject.setObjectId("33");     anObject.setUserId("4268321");     //... more     ObjectMapper mapper = new ObjectMapper();     mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);     ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();     String requestJson=ow.writeValueAsString(anObject );      mockMvc.perform(post(url).contentType(APPLICATION_JSON_UTF8)         .content(requestJson))         .andExpect(status().isOk()); } 

As described in the comments, this works because the object is converted to json and passed as the request body. Additionally, the contentType is defined as Json (APPLICATION_JSON_UTF8).

More info on the HTTP request body structure

like image 198
Priyanka Gupta Avatar answered Oct 21 '22 16:10

Priyanka Gupta