0

I would like to pass an input file to MockMVC perform statement. Please find the code snippet below:

@Test
public void test() throws Exception {

   this.mockMvc.perform(post("/tax_rates/v1/quotations")
           .contentType(MediaType.APPLICATION_JSON_UTF8).pathInfo("/src/main/resources/input.json"))
           .andExpect((ResultMatcher) status().is2xxSuccessful());

}

When I tried using pathInfo variable I get the error as below:

HttpMessageNotReadableException: Required request body is missing:

which i guess it means payload is not getting passed?

Any suggestions would help me.

Regards, Sunil

1 Answer 1

1

we can pass json input as content :

ObjectMapper mapper=new ObjectMapper();
String jsonString=mapperwriteValueAsString(mapper.readValue(new File("path/to/file",Object.class));
 this.mockMvc.perform(post("/tax_rates/v1/quotations")
           .contentType(MediaType.APPLICATION_JSON_UTF8).content(jsonString))
           .andExpect(status().is2xxSuccessful());

If you want to pass MultipartFile as input. Here is the link:

Using Spring MVC Test to unit test multipart POST request

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.