我正在使用 spring-data-jpa @EnableSpringDataWebSupport
和 DomainClassConverter
不通过存储库手动查找实例。当在 Controller 上进行 Controller 测试(MockMvc 独立设置测试)时
@RequestMapping(value = '/user/{userId}', method = RequestMethod.GET)
public UserDetails detail(@PathVariable('userId') User user) {
...
}
我得到一个
ConversionNotSupportedException
.是否可以像这样测试 Controller ?我该怎么办?
最佳答案
我不知道这会是一个选项,但在我的情况下,我曾经使用 HttpClient 通过 IntegrationTest 测试我的 Controller
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();
}
您也可以使用 Spring 的 Rest 模板 https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate
关于spring - MVC Controller 测试和 spring-data-jpa EnableSpringDataWebSupport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810648/