【发布时间】:2019-07-29 12:01:44
【问题描述】:
虽然响应状态为 200,但在进行内容协商测试时,模拟 GET 在响应正文中返回 null。
java.lang.AssertionError: Response header 'Content-Type'
Expected :application/json;charset=UTF-8
Actual :null
这里是完整的测试类代码。我想验证内容类型是 json。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
Controller controller;
@Test
public void test() throws Exception {
mockMvc.perform(get("/query?mediaType=json"))
.andExpect(status().isOk())
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE));
}}
这是我的控制器的端点。
@RestController
public class Controller {
@RequestMapping(value = "/query", produces = {"application/json", "application/xml"}, method = RequestMethod.GET)
public @ResponseBody ResultSet getResults(
final HttpServletRequest request
) throws Throwable {
// logic ...
SearchService search = (SearchService) context.getBean("search");
ResultSet result = search.getResults();
return result;
}
有什么想法为什么 Body 会返回 null 吗?
【问题讨论】:
标签: rest spring-boot spring-boot-test content-negotiation