如何模拟 CriteriaQuery.where() 后跟命令

How to mock CriteriaQuery.where() followed by order by

我有 class 其中

CriteriaQuery<Entity> cq=criteriaQuery.root<Entity>();
.....
.....
cq.where(predictes.toArray(new Predicate[0])).orderBy(criteriaBuilder.asc(root.get(ENTITY.COLUMNNAME))

我需要模拟这一行,无法通过这一行。 我试过了

mockito.doReturn(Path).when(root).get(....);
mockito.doReturn(Order).when(criteriabuilder).asc(Path);

这也没有帮助我实现

我建议模拟存储库方法而不是 CriteriaQuery 构造,在你的模拟中使用类似这样的东西 class:

    @Mock
    CustomRepository customRepository;
    Mockito.when(customRepository.findEntity(Matchers.any()))
            .thenReturn(entity);