【问题标题】:MockMvc empty response/returnMockMvc 空响应/返回
【发布时间】:2020-01-18 06:48:55
【问题描述】:

我刚刚使用 MockMvc 为控制器创建了一个简单的集成测试。一切正常,但即使控制器方法返回某些内容也没有提供响应。 这是控制器:

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);
}
}

这里是测试控制器:

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());
}
}

当我执行测试时,控制器方法被成功调用并返回它应该返回的内容:

        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);

但是在测试控制器中:mvcResult.getResponse().getContentAsString() 返回空字符串

测试有什么问题?

【问题讨论】:

  • 您是否在测试范围之外对其进行了测试,例如使用 Postman?这行得通吗?
  • 不,我没有作为独立应用程序进行部署和测试。我首先编写单元测试和集成测试,然后将应用程序部署到 Web 容器中。尽量确保功能符合预期,而不是试图弄清楚为什么它在部署时无法在实时运行中工作。
  • 那就很难帮你了。代码看起来不错,我可以想象在使用 ObjectMapper 将列表转换为 json 字符串进行映射时出现问题。我测试了一些类似的代码,它可以正常工作,你能验证 objectMapper 没问题吗?
  • 好的,我看看 ObjectMapper 是否一切正常
  • 好的,解决了!所以,对不起我!我确信我的 Class 上有 RestController ...。所以我在 Class 上没有 RestController,在方法级别上没有 ResponseBody。

标签: java rest spring-test-mvc


【解决方案1】:

方法上没有@ResponseBody 的错误。 因此,要么在 Class 上使用 @RestController,要么在 Class 上使用 @Controller,在方法上使用 @ResponseBody。

【讨论】:

    猜你喜欢
    • 2021-01-13
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多