Java 如何模拟CriteriaQuery.where()后跟order by
我在哪里上课Java 如何模拟CriteriaQuery.where()后跟order by,java,jpa,criteria,predicate,Java,Jpa,Criteria,Predicate,我在哪里上课 CriteriaQuery<Entity> cq=criteriaQuery.root<Entity>(); ..... ..... cq.where(predictes.toArray(new Predicate[0])).orderBy(criteriaBuilder.asc(root.get(ENTITY.COLUMNNAME)) 这也无助于我实现我建议使用mock repository方法而不是CriteriaQuery构造,在您的mock类中使用
CriteriaQuery<Entity> cq=criteriaQuery.root<Entity>();
.....
.....
cq.where(predictes.toArray(new Predicate[0])).orderBy(criteriaBuilder.asc(root.get(ENTITY.COLUMNNAME))
这也无助于我实现我建议使用mock repository方法而不是CriteriaQuery构造,在您的mock类中使用如下内容:
@Mock
CustomRepository customRepository;
Mockito.when(customRepository.findEntity(Matchers.any()))
.thenReturn(entity);
模拟
CriteriaQuery
构造细节的测试的价值让我无法信服。我认为您应该对CriteriaQuery的使用有一个更高的抽象,并模拟这个抽象。问题是说正在使用Spring(数据JPA)吗?不,问题不是说它使用Spring(数据JPA)。那么您为什么要回答Spring数据JPA JPA?在我的回答中,CustomRepository
的定义与实现无关,即findentity
方法可以使用Spring数据实现,也可以按要求使用标准实现。
@Mock
CustomRepository customRepository;
Mockito.when(customRepository.findEntity(Matchers.any()))
.thenReturn(entity);