Hi,
Could anyone help in writing Junit test case for AEM query builder API.
Thanks,
Divya
Solved! Go to Solution.
Views
Replies
Total Likes
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);
}
Views
Replies
Total Likes
You can refer to the Javadoc links for com.day.cq.search, which can be obtained here:
Then, you can use the API docs to create tests.
Ex: QueryBuilder Interface: https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/co...
You can make use of the methods available here and create tests using those methods.
The following links may be helpful:
https://www.programcreek.com/java-api-examples/?api=com.day.cq.search.QueryBuilder
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);
}
Views
Replies
Total Likes
Views
Replies
Total Likes
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);
}
}
Views
Replies
Total Likes