我正在尝试为使用 Querydsl (Mysema) 库的 Spring Boot 应用程序的方法设置单元测试。要测试的方法包括以下几行代码:
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
QEntity q = QEntity.entity;
long count = queryFactory.from(q)
.select(q.anInteger)
.where(aBooleanExpression)
.fetchCount();
在单元测试类中,我正在编写一个用@Before注释的设置方法,其中我执行以下操作:
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);
没有编译错误,但是当我运行测试时出现异常:
java.lang.ClassCastException: com.querydsl.core.support.QueryBase$$EnhancerByMockitoWithCGLIB$$6824f47d cannot be cast to com.querydsl.jpa.impl.JPAQuery
我不知道我必须以哪种方式对以前的代码进行处理才能使其正常工作。