简体   繁体   中英

MockMvc empty response/return

I just created a simple integration test with MockMvc for a controller. All works well, but there is no response provided even the controller method returns something. Here is the Controller:

import depmgmt.service.deposit.HouseService;
import depmgmt.service.dto.deposit.HouseDTO;
import depmgmt.serviceweb.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/rest/house")
public class HouseRestController extends BaseController {


private HouseService houseService;

@Autowired
public HouseRestController(HouseService houseService) {
    this.houseService = houseService;
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public List<HouseDTO> getHouses(@RequestParam(value = "page", defaultValue = "1") int page, 
@RequestParam(value = "size", defaultValue = "50") int size) {
    validatePageRequest(page, size);
    return houseService.getHouseList(page, size);
}
}

Here is the test controller:

import com.fasterxml.jackson.databind.ObjectMapper;
import depmgmt.service.deposit.HouseService;
import depmgmt.service.dto.deposit.HouseDTO;
import depmgmt.serviceweb.config.SpringServiceWebConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {SpringServiceWebConfig.class})
@WebMvcTest(controllers = HouseRestController.class)
public class HouseRestControllerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@MockBean
private HouseService houseService;


@Test
public void shouldGetHouses() throws Exception {
    //given
    List<HouseDTO> houseDTOS = new ArrayList<>();
    HouseDTO houseDTO = HouseDTO.builder().country(null).city("City").owner(null).name("MyHouse").id(1l).address("HouseAddress").build();
    houseDTOS.add(houseDTO);
    when(houseService.getHouseList(1, 1)).thenReturn(houseDTOS);
    //when
    MvcResult mvcResult = mockMvc.perform(get("/rest/house/")
            .contentType("application/json")
            .param("page", "1")
            .param("size", "1")).andReturn();

    System.out.println(mvcResult.getResponse().getContentAsString());
}
}

When I execute the test the controller method is called successfully and it returns what it should:

        HouseDTO houseDTO = HouseDTO.builder().country(null).city("City").owner(null).name("MyHouse").id(1l).address("HouseAddress").build();
    houseDTOS.add(houseDTO);
    when(houseService.getHouseList(1, 1)).thenReturn(houseDTOS);

在此处输入图片说明

However in the test Controller the: mvcResult.getResponse().getContentAsString() returns empty string :

What is wrong in the test?

The mistake what that there is no @ResponseBody on the method. So either @RestController on the Class or @Controller on the class and @ResponseBody on the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM