问题描述
我在spring-application中有一个restcontroller,它返回对象列表...
I have a restcontroller inside a spring-application returning a list of objects...
@GetMapping
@Override
public ResponseEntity readAll(@QuerydslPredicate(root = Entity.class) Predicate predicate, Pageable pageable){
...
}
如果我运行它,则一切正常.我可以按分页和谓词过滤请求.但是,如果我运行junit测试,它将失败...
If I run it, everything works fine. I can filter the request by pageable and predicate. But if I run the junit test, it fails...
@Test
public void readAllTest(){
MockMvcBuilders.standaloneSetup(*myController*)
.build().perform(MockMvcRequestBuilders.get(*myUri*)
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
}
获取以下错误消息... org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为java.lang.IllegalStateException:找不到接口com.querydsl.core.types.Predicate
Getting the following Errormessage...org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface com.querydsl.core.types.Predicate
有人知道如何使用Pageable和Predicate测试Restcontroller吗?
Does anybody know how to test a restcontroller with a Pageable and Predicate?
推荐答案
-
尝试在测试类注释上添加 @Import(QuerydslWebConfiguration.class) .它将 com.querydsl.core.types.Predicate 的控制器参数解析器添加到Spring上下文中.
Try to add on your test class annotation @Import(QuerydslWebConfiguration.class). It adds controller argument resolver for com.querydsl.core.types.Predicate into spring context.
但是在遇到类似以下的异常之后:
But after you'll face with an exception like:
找不到接口org.springframework.data.domain.Pageable的主要或默认构造函数.
No primary or default constructor found for interface org.springframework.data.domain.Pageable.
有注释,为这两个接口加载参数解析器. org.springframework.data.web.config.EnableSpringDataWebSupport
There is annotation, loads argument resolvers for both these interfaces. org.springframework.data.web.config.EnableSpringDataWebSupport
已针对您的测试课程进行了调整:
Addapted for your test class:
@RunWith(SpringRunner.class)
@WebMvcTest(*myController*.class)
@EnableSpringDataWebSupport
public class ControllerTest{
@Test
public void readAllTest(){
MockMvcBuilders.standaloneSetup(*myController*)
.build().perform(MockMvcRequestBuilders.get(*myUri*)
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
}
}
这篇关于jUnit:找不到接口com.querydsl.core.types.Predicate的主要或默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!