Unit testing QueryBuilder and Query | Community
Skip to main content
New Participant
February 6, 2023
Solved

Unit testing QueryBuilder and Query

  • February 6, 2023
  • 1 reply
  • 1556 views

What is the proper way to unit test QueryBuilder and Query?  Can I feed in mock data to the AemContext and have QueryBuilder execute against it?

 

Are there any examples or best practices that can be followed?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Saravanan_Dharmaraj

Do you have the unit test class with these annotations? 

 

@ExtendWith({ AemContextExtension.class, MockitoExtension.class })
@MockitoSettings(strictness = Strictness.LENIENT)
class UnitTest {
}

The same code works for me. 

1 reply

Saravanan_Dharmaraj
New Participant
February 6, 2023

Some sample code to start with

 

 

public static final String BASE_PAGE_PATH = "/content/we.retail/en"; AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK); @Mock private QueryBuilder queryBuilder; @Mock private Query query; @Mock private SearchResult searchResult; @BeforeEach void setUp() throws Exception{ aemContext.registerService(QueryBuilder.class, queryBuilder); aemContext.load().json("/page.json", BASE_PAGE_PATH); configureQueryBuilder(); } public void configureQueryBuilder() throws Exception { final List<Resource> results = new ArrayList<>(); Resource pageResult1 = mock(Resource.class); Resource pageResult2 = mock(Resource.class); pageResult1 = aemContext.resourceResolver().getResource(BASE_PAGE_PATH+"/page1"); pageResult2 = aemContext.resourceResolver().getResource(BASE_PAGE_PATH+"/page1"); results.add(pageResult1); results.add(pageResult2); // More Generic mock of querybuilder calls lenient().when(queryBuilder.createQuery(any(PredicateGroup.class), any(Session.class))).thenReturn(query); lenient().when(query.getResult()).thenReturn(searchResult); lenient().when(searchResult.getResources()).thenReturn(results.iterator()); }

 

  

New Participant
February 7, 2023

So I've tried this, but this line of code returns null within my service:

 

Query query = queryBuilder.createQuery(PredicateGroup.create(paramMap), resolver.adaptTo(Session.class));

 

After this runs under the unit test, query is null- so future calls like query.setStart() throw a null reference exception.
 
In my configureQueryBuilder, I have it looking like this:
 
public void configureQueryBuilder() throws Exception { final List<Resource> results = new ArrayList<>(); Resource pageResult1 = mock(Resource.class); Resource pageResult2 = mock(Resource.class); pageResult1 = aemContext.resourceResolver().getResource(BASE_PAGE_PATH+"/page-1"); pageResult2 = aemContext.resourceResolver().getResource(BASE_PAGE_PATH+"/page-2"); results.add(pageResult1); results.add(pageResult2); doNothing().when(query).setStart(isA(long.class)); lenient().when(query.getResult()).thenReturn(searchResult); // More Generic mock of querybuilder calls lenient().when(queryBuilder.createQuery(any(PredicateGroup.class), any(Session.class))).thenReturn(query); lenient().when(searchResult.getResources()).thenReturn(results.iterator()); }
 
how do I get the Mock query object to return?  Maybe the mock is returning, but the Mock is null?
Saravanan_Dharmaraj
Saravanan_DharmarajAccepted solution
New Participant
February 7, 2023

Do you have the unit test class with these annotations? 

 

@ExtendWith({ AemContextExtension.class, MockitoExtension.class })
@MockitoSettings(strictness = Strictness.LENIENT)
class UnitTest {
}

The same code works for me.