I am trying to set up an unit test for a method of an Spring Boot application that use Querydsl (Mysema) library. The method to be tested include the following lines of code:
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
QEntity q = QEntity.entity;
long count = queryFactory.from(q)
.select(q.anInteger)
.where(aBooleanExpression)
.fetchCount();
In the unit test class i am writting a set up method annotated with @Before where i do the following:
JPAQueryFactory queryFactory = Mockito.mock(JPAQueryFactory.class, Mockito.RETURNS_DEEP_STUBS);
QEntity q = QEntity.etity;
BooleanExpression aBooleanExpression = ... // The same as used in the method under test
Mockito
.when(((JPAQuery<Integer>) queryFactory
.from(q)
.select(q.anInteger))
.where(aBooleanExpression)
.fetchCount()
).thenReturn(1L);
There is no compilation errors, but when i run the test an get an exception:
java.lang.ClassCastException: com.querydsl.core.support.QueryBase$$EnhancerByMockitoWithCGLIB$$6824f47d cannot be cast to com.querydsl.jpa.impl.JPAQuery
I don't know in which manner i must refractory the previous code to make it works.