MVC Controller测试和spring-data-jpa EnableSpringDataWebSupport

Mat*_*ral 6 spring spring-mvc spring-data-jpa

我正在使用spring-data-jpa @EnableSpringDataWebSupport并且DomainClassConverter不通过存储库手动查找实例.当在控制器上做一个控制器测试(MockMvc独立设置测试)时

@RequestMapping(value = '/user/{userId}', method = RequestMethod.GET)
public UserDetails detail(@PathVariable('userId') User user) {
...

}
Run Code Online (Sandbox Code Playgroud)

我得到了ConversionNotSupportedException.有可能像这样测试控制器吗?我该怎么办?

pau*_*aul 0

我不知道这会是一个选项,但就我而言,我曾经使用 HttpClient 通过 IntegrationTest 测试我的控制器

 HttpClient httpClient = login(HTTP_SERVER_DOMAIN, "user1@gmail.com", "password");
    GetMethod getAllAdvicesMethod = new GetMethod(adviceGetURL);
    getAllAdvicesMethod
            .addRequestHeader("Content-Type", "application/json");
    try {
        httpClient.executeMethod(getAllAdvicesMethod);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

您也可以使用 Spring 的 Rest 模板https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate