Leiste mit Community-Erfolgen erweitern.

Get ready! An upgraded Experience League Community experience is coming in January.

Mark Solution

Diese Konversation wurde aufgrund von Inaktivität geschlossen. Bitte erstellen Sie einen neuen Post.

GELÖST

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 Akzeptierte Lösung

Avatar

Korrekte Antwort von
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.

 

Lösung in ursprünglichem Beitrag anzeigen

2 Antworten

Avatar

Level 8

Any help on this??

Avatar

Korrekte Antwort von
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.