Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

Unit Test for for SlingServlet


Level 5

Hi All

I need to write a UnitTest for a doGet method in a Servlet that extends SlingSafeMethodsServlet. 


This is the doGet method I need to write the UnitTest for and I am not sure how to mock the resource for it:

protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws IOException {
final JsonResponse jsonResponse = new JsonResponse(response);
final String title = request.getParameter("title");
final List<APIResult> locations = LocationFinderUtil.getLocationResultStream(request, searchService)
.filter(result -> StringUtils.isNotBlank(title) &&
(StringUtils.containsIgnoreCase(result.getTitle(), title) || StringUtils.containsIgnoreCase(result.getAdditionalAddress(), title)))


1 Accepted Solution


Community Advisor
7 Replies


Level 5

Hi @Hemalatha 

Thanks for the reply. 

I am sorry but these two links are broken:


If I use the AEMContext to mock the resource how do I build it using



Level 4

Hi @anasustic ,


The links are working. PFA. May be it was blocked if you are using VPN. Kindly check. Understand the Junit samples for servlet. How it has been written and note down the test cases for your servlet based on your use case, then write it by referring the samples is the best way. 




Level 5

Thank you very much @Hemalatha I will research the links from another location. It appears not to work from my current location.


Level 5

Thanks @arunpatidar 

Your example is clear. I do however build a resource that is a page with several components. I am not sure if I can build a complex resource with context.


Community Advisor