0

I have a method that uses queryDSL for query generation.

public List<EntityDAO> getObject() {
        QEntity entity = QEntity.entity;
        JPAQueryFactory queryFactory = getJPAQueryFactory();

        JPAQuery<EntityDAO> query = queryFactory
                             .select(Projections.bean(EntityDAO.class, 
                              entity.propertyA,
                              entity.propertyB.count().as("count")))
                             .from(entity)
                             .where(predicateBuilder.build())
                             .groupBy(entity.propertyA)
                             .orderBy(order)
                             .limit(rowCount)
                             .offset(pageId*rowCount);
       return query.fetch();
}

How can I test this method using Mockito?

avi
  • 101
  • 4
  • 13

1 Answers1

0

This method is the data access layer. For testing the data layer access codes , it is best to test it with the actual DB instance but not a mock because in the end you still need to verify if it can really get the data from the actual database correctly and this is the most appropriate place to do it.

You can check with Testcontainers. It allows you to test with a containerised instance of your DB. After starting this DB container , you simply load some testing data to related tables , call this method and directly verify the correctness of the return data.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172