Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing HttpMediaTypeNotSupportedException在mockmvc post上_Unit Testing_Spring Boot_Mime Types_Mockmvc - Fatal编程技术网

Unit testing HttpMediaTypeNotSupportedException在mockmvc post上

Unit testing HttpMediaTypeNotSupportedException在mockmvc post上,unit-testing,spring-boot,mime-types,mockmvc,Unit Testing,Spring Boot,Mime Types,Mockmvc,我有一个RestController和一个接受post请求的函数 @RequestMapping(path = "/auth",method = RequestMethod.POST) public void authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse httpServletResponse) throws IOException { } 我试图发出一个p

我有一个RestController和一个接受post请求的函数

@RequestMapping(path = "/auth",method = RequestMethod.POST)
public void authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse httpServletResponse) throws  IOException {
}
我试图发出一个post请求

mockMvc.perform(post("/auth")
                    .contentType(MediaType.APPLICATION_JSON)
                    .content("{ \"foo\": \"bar\", \"fruit\": \"apple\" }".getBytes()))
            .andDo(print());
我收到

Resolved Exception:
         Type = org.springframework.web.HttpMediaTypeNotSupportedException
有解决办法吗


编辑:我还尝试在控制器上指定consumes=“application/json”,但仍然不起作用

例外情况是不接受“媒体类型”即“内容类型”

尝试将consumes=“application/json”添加到控制器函数中

@RequestMapping(path = "/auth",method = RequestMethod.POST,consumes = "application/json")
public void authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse httpServletResponse) throws  IOException {
}

有关详细信息,请参阅spring文档

也许您可以提供整个异常以及更多的上下文?