Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Can not attach body to my POST request using Spring MockMvc

I'm trying to test my rest controller. No issues with GETs, but when I try to test a POST method I'm unable to attach the body.

private static final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                                                            MediaType.APPLICATION_JSON.getSubtype(),
                                                            Charset.forName("utf8"));
private ObjectMapper jsonMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);

@Test
public void test1() throws Exception {
    //...Create DTO
    //...Create same pojo but as entity

    when(serviceMock.addEntity(e)).thenReturn(e);

    mvc.perform(post("/uri")
        .contentType(contentType)
        .content(jsonMapper.writeValueAsString(dto))
        )
        .andDo(print())
        .andExpect(status().isCreated())
        .andExpect(content().contentType(contentType)); //fails because there is no content returned
}

This is the request output:

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /uri
       Parameters = {}
          Headers = {Content-Type=[application/json;charset=UTF-8]}

There is no body. Why? I have printed jsonMapper.writeValueAsString(dto) and is not null.

edit:

Adding controller code:

@RestController
@RequestMapping("/companies")
public class CompanyController {

    @Autowired
    private CompanyService service;
    @Autowired
    private CompanyMapper mapper;



    @RequestMapping(method=RequestMethod.GET)
    public List<CompanyDTO> getCompanies() {
        List<Company> result = service.getCompanies();
        return mapper.toDtoL(result);
    }   

    @RequestMapping(method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public CompanyDTO createCompany(@RequestBody @Valid CompanyDTO input) {
        Company inputE = mapper.toEntity(input);
        Company result = service.addCompany(inputE);
        return mapper.toDto(result);
    }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Solved.

  1. The mock call should use any instead of a concrete object: when(serviceMock.addCompany(any(Company.class))).thenReturn(e);

  2. I needed to override the equals method of the entity class to pass this statement: verify(serviceMock, times(1)).addCompany(e);

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!