Junit Test case - Query builder API

Avatar

Avatar

divyas70997717

Avatar

divyas70997717

divyas70997717

20-01-2020

Hi,

 

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

 

Thanks,

Divya

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Reply

838

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Reply

838
Arun_Patidar
MVP

20-01-2020

 

 

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);
}

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

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);
}
}

Answers (1)

Answers (1)

Avatar

Avatar

sunjot16

Employee

Avatar

sunjot16

Employee

sunjot16
Employee

20-01-2020

You can refer to the Javadoc links for com.day.cq.search, which can be obtained here:

https://docs.adobe.com/content/help/en/experience-manager-64/developing/platform/query-builder/query...

 

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

https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad...