Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Junit Test case - Query builder API

Avatar

Level 3

Hi,

 

Could anyone help in writing Junit test case for AEM query builder API.

 

Thanks,

Divya

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

 

 

Map<String, String> map = new HashMap<>();
map.put("group.p.or", "true");
map.put("group.1_property", JcrConstants.JCR_TITLE);
map.put("group.1_property.value", "true");
map.put("group.1_property.operation", "exists");
map.put("group.2_property", MyConstants.TITLE);
map.put("group.2_property.value", "true");
map.put("group.2_property.operation", "exists");
query = mock(Query.class);
QueryBuilder queryBuilder = mock(QueryBuilder.class);
SlingRepository repository = mock(SlingRepository.class);
PrivateAccessor.setField(mySearchImpl, "queryBuilder", queryBuilder);
PrivateAccessor.setField(mySearchImpl, "repository", repository);
Mockito.when(queryBuilder.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class))).thenReturn(query);
}



Arun Patidar

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

 

 

Map<String, String> map = new HashMap<>();
map.put("group.p.or", "true");
map.put("group.1_property", JcrConstants.JCR_TITLE);
map.put("group.1_property.value", "true");
map.put("group.1_property.operation", "exists");
map.put("group.2_property", MyConstants.TITLE);
map.put("group.2_property.value", "true");
map.put("group.2_property.operation", "exists");
query = mock(Query.class);
QueryBuilder queryBuilder = mock(QueryBuilder.class);
SlingRepository repository = mock(SlingRepository.class);
PrivateAccessor.setField(mySearchImpl, "queryBuilder", queryBuilder);
PrivateAccessor.setField(mySearchImpl, "repository", repository);
Mockito.when(queryBuilder.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class))).thenReturn(query);
}



Arun Patidar

Avatar

Level 3
Thank you Arun for your reply. May i know what is exactly the variable "mySearchImpl" about?

Avatar

Community Advisor

mySearchImpl is the mysearch osgi service implementation

 

@RunWith(JUnitPlatform.class)
class MySearchImplTest {

private Query query;

@test
void testGetMyListforEmptyResult() {
Session session = mock(Session.class);
MyArticle myArticle = mySearchImpl.getResultList(session, "mykeyword", "1995-05-05", "1996-05-05");
assertEquals(0, myArticle.getCount());
}

@InjectMocks
private MySearchImpl mySearchImpl = new MySearchImpl();

@BeforeEach
void setup() {
try {
initMock();
} catch (NoSuchFieldException | RepositoryException e) {
e.getCause();
}
}

private void initMock() throws NoSuchFieldException, LoginException, RepositoryException {
Map<String, String> map = new HashMap<>();
map.put("group.p.or", "true");
map.put("group.1_property", JcrConstants.JCR_TITLE);
map.put("group.1_property.value", "true");
map.put("group.1_property.operation", "exists");
map.put("group.2_property", MYConstants.TITLE);
map.put("group.2_property.value", "true");
map.put("group.2_property.operation", "exists");
query = mock(Query.class);
QueryBuilder queryBuilder = mock(QueryBuilder.class);
SlingRepository repository = mock(SlingRepository.class);
PrivateAccessor.setField(mySearchImpl, "queryBuilder", queryBuilder);
PrivateAccessor.setField(mySearchImpl, "repository", repository);
Mockito.when(queryBuilder.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class))).thenReturn(query);
}
}



Arun Patidar