MVC 控制器测试和 spring-data-jpa EnableSpringDataWebSupport [英] MVC Controller test and spring-data-jpa EnableSpringDataWebSupport
问题描述
我使用 spring-data-jpa @EnableSpringDataWebSupport
和 DomainClassConverter
不通过存储库手动查找实例.当在控制器上进行控制器测试(MockMvc 独立设置测试)时,如
I'm using spring-data-jpa @EnableSpringDataWebSupport
and DomainClassConverter
to not manually lookup the instances via the repository. When a do a controller test (MockMvc standalone setup test) on a controller like
@RequestMapping(value = '/user/{userId}', method = RequestMethod.GET)
public UserDetails detail(@PathVariable('userId') User user) {
...
}
我收到一个 ConversionNotSupportedException
.是否可以像这样测试控制器?我该怎么办?
I get a ConversionNotSupportedException
. Is it possible to test controllers like this? What should I do?
推荐答案
我不知道这会是一个选项,但在我的例子中,我曾经使用 HttpClient 通过 IntegrationTest 测试我的控制器
I dont know it would be an option, but in my case I used to use HttpClient to test my controllers with 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();
}
您也可以使用 Spring 的 Rest 模板 https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate
You can use Rest Template for Spring as well https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate
这篇关于MVC 控制器测试和 spring-data-jpa EnableSpringDataWebSupport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!