完成测试控制器后,响应主体为空

时间:2019-05-25 18:44:21

标签: spring-boot testing mocking

我的请求有一个正文并且测试已完成,但是我需要测试响应正文,但这是空的,为什么?

@RunWith(SpringRunner.class)
@WebMvcTest(RentalController.class)
public class RentalControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private RentalService rentalService;

    @MockBean
    private RentalMapper rentalMapper;

    @Test
    public void shouldReturnRentalByUsername() throws Exception {

        User user = new User(1L,"janek22", "janek", "janek2@wp.pl");
        Bike bike = new Bike("AB123", false);
        LocalDateTime dateTime = LocalDateTime.of(2019,5,24,1,0,0);
        Rental rental = new Rental(user, bike, dateTime , dateTime,
                "22.222", "33.444", "33.444", "33.444", "33.444" );
        given(rentalService.findByUsername(anyString())).willReturn(Collections.singletonList(rental));

        ObjectMapper objectMapper = new ObjectMapper();
        String content = objectMapper.writeValueAsString(user);

        mockMvc.perform(get("/rental/" + user.getUsername())
                    .contentType(MediaType.APPLICATION_PROBLEM_JSON_UTF8_VALUE)
                    .content(content))
                .andExpect(status().isOk())
                .andDo(print());



    }
}

MockHttpServletResponse:
           状态= 200
    错误消息= null
          标头= [Content-Type:“ application / json; charset = UTF-8”]
     内容类型= application / json; charset = UTF-8
             正文= []
    转发的网址= null
   重定向的URL = null
          Cookies = []

0 个答案:

没有答案