4

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.

Diego Cairone
  • 93
  • 4
  • 10

2 Answers2

1

Every call return object

queryFactory.from(q)
            .select(q.anInteger))
            .where(aBooleanExpression)
            .fetchCount()

in your case you should mock step by step:

 JPAQuery step1 = Mockito.mock(JPAQuery.class);
 Mockito.when(queryFactory.from(q)).thenReturn(step1);
 JPAQuery step2 = Mockito.mock(JPAQuery.class);
 Mockito.when(step1.select(q.anInteger)).thenReturn(step2);
 JPAQuery step3 = Mockito.mock(JPAQuery.class);
 Mockito.when(step2.where(aBooleanExpression)).thenReturn(step3);

... and so on

Not sure about return object class please check, it is example just for explanations.

borino
  • 1,720
  • 1
  • 16
  • 24
0

I have faced this problem with Springboot unitest, my query is

List<TaxiEntity> taxiEntities = queryFactory.selectFrom(qTaxiEntity)
            .innerJoin(qTaxiEntity.callMe, qDriver)
            .where(qTaxiEntity.abcId.eq(abcId))
            .limit(pageable.getPageSize())
            .offset(pageable.getOffset())
            .fetch();

my unittest to mock this query is. It run successful

    @Mock
    private JPAQueryFactory queryFactory;

    @Test
    public void testBanana() {
    .....
    JPAQuery jpaQuery = Mockito.mock(JPAQuery.class);
    when(queryFactory.selectFrom(any())).thenReturn(jpaQuery);
    when(jpaQuery.select(any(Expression.class))).thenReturn(jpaQuery);
    when(jpaQuery.innerJoin((EntityPath) any(), any())).thenReturn(jpaQuery);
    when(jpaQuery.where(any(BooleanExpression.class))).thenReturn(jpaQuery);
    when(jpaQuery.limit(anyLong())).thenReturn(jpaQuery);
    when(jpaQuery.offset(anyLong())).thenReturn(jpaQuery);
    when(jpaQuery.fetch()).thenReturn(resultList);
    ....
    }
Tung Vo
  • 2,227
  • 5
  • 27
  • 45