简体   繁体   中英

How to Mock a CriteriaQuery.where(EntityRoot.get(ID).in(List<Integer>)

I am trying to Mock the function block

    @Mock
    private EntityManager entityManager;

    @Mock
    private CriteriaBuilder criteriaBuilder;

    @Mock
    CriteriaQuery<Entity> criteriaQuery;

    @Mock
    TypedQuery<Entity> typedQuery;

    @Mock
    Predicate predicate;

    @Mock
    Root<Entity> EntityRoot;



 criteriaQuery.where(criteriaBuilder.and(
    EntityRoot.get(ID).in(request.getIdList()),
    criteriaBuilder.greaterThanOrEqualTo(EntityRoot.get(columnName),
                                    Request.getTimeRangeFilter().getStartDate())));

ID - Static String

i tried mocking with the below code

when(criteriaQuery.where(criteriaBuilder.and(predicate, predicate))).thenReturn(criteriaQuery);

but for the line EntityRoot.get(ID).in(request.getIdList())

i get the below error

在此处输入图像描述

What am i doing wrong here

@Mock
Path<Object> path; 

when(EntityRoot.get(anyString())).thenReturn(path);
        when(path.in(Mockito.anyList())).thenReturn(predicate);

This solution worked finally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM