Accepted Solutions (1)

Accepted Solutions (1)

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