title: Spring boot @MockMvcTest MockHttpServletResponse always returns empty body
body: <p>I'm struggling with a simple spring boot rest controller test which always return empty body response.</p>
  <p>Here is my test code looks like:</p>
  <pre class="lang-java prettyprint-override"><code>@WebMvcTest(AdminRestController.class)
  @AutoConfigureMockMvc(addFilters = false)
  public class PatientsUnitTest {
  
      @Autowired
      private MockMvc mvc;
  
      @Autowired
      private ObjectMapper objectMapper;
  
      @MockBean
      private PatientsService patientsService;
  
      @MockBean
      private TherapistsService therapistsService;
  
      @MockBean
      private TherapySchedulesService therapySchedulesService;
  
      @Test
      public void canAddPatient() throws Exception {
          PatientsSaveRequestDto patientsSaveRequestDto = new PatientsSaveRequestDto();
          patientsSaveRequestDto.setName(&quot;Sofia&quot;);
          patientsSaveRequestDto.setPhone(&quot;01012345678&quot;);
          Patients patient = patientsSaveRequestDto.toEntity();
  
          when(patientsService.createPatient(patientsSaveRequestDto)).thenReturn(patient);
  
          final ResultActions actions = mvc.perform(post(&quot;/admin/patient&quot;)
                  .contentType(MediaType.APPLICATION_JSON_VALUE)
                  .characterEncoding(StandardCharsets.UTF_8.name())
                  .content(objectMapper.writeValueAsString(patientsSaveRequestDto)))
                  .andDo(print());
  
          actions
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                  .andExpect(jsonPath(&quot;name&quot;, is(patient.getName())))
                  .andDo(print());
      }
  </code></pre>
  <p>My Controller:</p>
  <pre class="lang-java prettyprint-override"><code>@RestController
  @RequiredArgsConstructor
  public class AdminRestController {
      private final PatientsService patientsService;
      private final TherapistsService therapistsService;
      private final TherapySchedulesService therapySchedulesService;
  
      @PostMapping(&quot;/admin/patient&quot;)
      @ResponseStatus(HttpStatus.OK)
      @Operation(summary = &quot;Create a patient&quot;)
      public Patients cratePatient(
              @RequestBody @Valid PatientsSaveRequestDto patientsSaveRequestDto
      ) {
          return patientsService.createPatient(patientsSaveRequestDto);
      }
  
  // PatientsService
  @Transactional
      public Patients createPatient(PatientsSaveRequestDto patientsSaveRequestDto){
          return patientsRepository.save(patientsSaveRequestDto.toEntity());
      }
  </code></pre>
  <p>And this is the result of print():</p>
  <pre><code>MockHttpServletRequest:
        HTTP Method = POST
        Request URI = /admin/patient
         Parameters = {}
            Headers = [Content-Type:&quot;application/json;charset=UTF-8&quot;, Content-Length:&quot;53&quot;]
               Body = {&quot;name&quot;:&quot;sofia&quot;,&quot;phone&quot;:&quot;01012345678&quot;,&quot;tel&quot;:null}
      Session Attrs = {}
  
  Handler:
               Type = com.ussoft.dosu.web.controller.admin.AdminRestController
             Method = com.ussoft.dosu.web.controller.admin.AdminRestController#cratePatient(PatientsSaveRequestDto)
  
  Async:
      Async started = false
       Async result = null
  
  Resolved Exception:
               Type = null
  
  ModelAndView:
          View name = null
               View = null
              Model = null
  
  FlashMap:
         Attributes = null
  
  MockHttpServletResponse:
             Status = 200
      Error message = null
            Headers = []
       Content type = null
               Body = 
      Forwarded URL = null
     Redirected URL = null
            Cookies = []
  </code></pre>
  <p>As you can see the request is sent correctly but the response values are all null.</p>
  <p>When i test the same controller using @SpringBootTest with Rest Assured it works fine.</p>
  <p>I'm using Spring boot 2.3.1, Junit5</p>
  <hr />
  <p>Edit - added PatientsSaveRequestDto</p>
  <pre class="lang-java prettyprint-override"><code>@Getter
  @Setter
  @NoArgsConstructor
  public class PatientsSaveRequestDto {
      @NotBlank(message = &quot;이름은 필수 입력사항입니다.&quot;)
      private String name;
  
      private String phone;
  
      private String tel;
  
      public Patients toEntity(){
          return Patients.builder()
                  .name(name)
                  .phone(phone)
                  .tel(tel)
                  .build();
      }
  }
  </code></pre>