Expand my Community achievements bar.

SOLVED

AEM6.5 Junit with MockitoJUnitRunner for NodeIterator and user

Avatar

Level 8

Hi,

I am using AEM6.5 Junit with MockitoJUnitRunner

 

I need to mock 

List<Node> resultList = new ArrayList<Node>();

String queryString = "select * from [rep:User] where ISDESCENDANTNODE([/home/users]) and [profile/givenName] = '"
+ firstName + "' and [profile/familyName] = '" + lastName + "'";

Query query = session.getWorkspace().getQueryManager().createQuery(queryString, javax.jcr.query.Query.JCR_SQL2);
QueryResult result = query.execute();
NodeIterator cNode = result.getNodes();
while (cNode.hasNext()) {
Node rNode = cNode.nextNode();
resultList.add(rNode);
}


for (Node node : resultList) {
userId = node.getProperty("rep:principalName").getValue().getString();
}
UserManager user = resourceResolver.adaptTo(UserManager.class);

if (userId != null) {
Authorizable authorizable = user.getAuthorizable(userId);
if (authorizable != null) {
Value[] emailProperty = authorizable.getProperty("./profile/email");
if (null != emailProperty) {
userObj.put("email", emailProperty[0].getString());
}

}
}

 

I tried with Test class:-


@Deleted Account
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

@Mock
protected Query mockQuery;

@Mock
protected QueryResult queryResult;

@Mock
protected NodeIterator mockNodeIt;

 

Could you please help with how could I write the junit for this whole method with a sample junit code will be helpfull.

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Try this:

@rule
public final AemContext context = new AemContext(ResourceResolverType.JCR_OAK);

(You need to have an additional dependency in your pom.xml, see https://sling.apache.org/documentation/development/sling-mock.html for more details.)

 

The reason is that other Types do not provide the full JCR semantic; and for these cases you need to use a full OAK implementation.

 

View solution in original post

2 Replies

Avatar

Correct answer by
Employee Advisor

Try this:

@rule
public final AemContext context = new AemContext(ResourceResolverType.JCR_OAK);

(You need to have an additional dependency in your pom.xml, see https://sling.apache.org/documentation/development/sling-mock.html for more details.)

 

The reason is that other Types do not provide the full JCR semantic; and for these cases you need to use a full OAK implementation.