gpt4 book ai didi

java - MockMvc 空响应/返回

转载 作者:行者123 更新时间:2023-12-02 00:35:59 30 4
gpt4 key购买 nike

我刚刚使用 MockMvc 为 Controller 创建了一个简单的集成测试。一切正常,但即使 Controller 方法返回某些内容也没有任何响应。这是 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);
}
}

这是测试 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());
}
}

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

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

enter image description here

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

测试中有什么问题?

最佳答案

错误是方法上没有@ResponseBody。因此,要么在类上使用@RestController,要么在类上使用@Controller,然后在方法上使用@ResponseBody。

关于java - MockMvc 空响应/返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57973672/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com