AEM Unit Test for a component that uses a Service returning a list of paths based on a Query | Community
Skip to main content
Level 6
January 20, 2023
Solved

AEM Unit Test for a component that uses a Service returning a list of paths based on a Query

  • January 20, 2023
  • 1 reply
  • 1012 views

Hi

I need to write a Unit Test for an AEM component that uses a service that returns a list of paths based on a Query. 

 

How do I write a Unit Test for such a method.

 

getBlogItemsForPaths(someService.findPages(filter), resource, blogContext);

 the someService returns a list of paths:

 

public List<String> someService(@Nonnull BlogArticleFilter filter) {
if (filter.getTagList().isEmpty()) {
return Collections.emptyList();
}

final String pathQuery = buildDesendantNodeString(filter.getPathList());
final String tagQuery = buildTagQuery(filter);
final String query = String.format(QUERY, pathQuery, Constants.TemplateType.BLOG_ARTICLE_PAGE.getTemplatePath(), tagQuery);

return resourceResolverUsageService.withServiceUserResourceResolver(ServiceUser.CONTENT_READER, resolver -> {
return StreamUtil.iteratorStream(resolver.findResources(query, Query.JCR_SQL2))
.sorted(Comparator.comparing(this::getDate, Comparator.reverseOrder()))
.map(Resource::getPath)
.filter(s -> !filter.getIgnoreList().contains(s))
.limit(filter.getMaxItems())
.collect(Collectors.toList());
});
}

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Peter_Puzanovs

Hi Anasustic,

 

Mockito is a wonderful library that let's you Mock any third party service that lives outside of your main class.

 

You can use @Mock annotation to mock any service you do not want to test in this particular tests and inject these into your @2785667 with @RunWith(MockitoJunitRunnr.class) comand.

 

Some useful tutorials[1] [2]

[1] java - How to use Mockito with JUnit5 - Stack Overflow

[2] Unit testing | Adobe Experience Manager

 

Regards,

Peter

1 reply

Peter_Puzanovs
Community Advisor
Peter_PuzanovsCommunity AdvisorAccepted solution
Community Advisor
January 20, 2023

Hi Anasustic,

 

Mockito is a wonderful library that let's you Mock any third party service that lives outside of your main class.

 

You can use @Mock annotation to mock any service you do not want to test in this particular tests and inject these into your @2785667 with @RunWith(MockitoJunitRunnr.class) comand.

 

Some useful tutorials[1] [2]

[1] java - How to use Mockito with JUnit5 - Stack Overflow

[2] Unit testing | Adobe Experience Manager

 

Regards,

Peter