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
BedrockMission!

Learn More

View all

Sign in to view all badges

Unit Testing Hands on - For Sling Servlet | AEM Community Blog Seeding

Avatar

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,162 likes

Total Posts

6,273 posts

Correct Reply

1,144 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,162 likes

Total Posts

6,273 posts

Correct Reply

1,144 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile
kautuk_sahni
Community Manager

28-07-2020

BlogImage.jpg

Unit Testing Hands on - For Sling Servlet by MyAEMLearnings Blog

Abstract

This post is about creating Unit Test class for Sling Servlet, another commonly used Java class as part of an AEM application.

In Sling servlets, we have
-->SlingSafeMethodsServlet - read only servlet supporting GET (doGet)
-->SlingAllMethodsServlet - Supports POST, PUT and DELETE (doPost/doPut/doDelete)

In either case, we have request and response objects using which desired code logic is written in Servlet.
For Unit testing, we have Mock sling request and sling response from Sling Mocks (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest and org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse respectively)

Writing unit test for Servlet involves the following
-->Instantiate Servlet to be tested.
-->Get mock request and response from AemContext
-->Prepare the mock request object (per the code logic before calling doGet/doPost)
-->Call doGet/doPost method of Servlet to be tested
-->Assert the Servlet response (per the code logic)

Read Full Blog

Unit Testing Hands on - For Sling Servlet

Q&A

Please use this thread to ask the related questions.

AEM AEMEBlogSeeding Experience Manager