Highlighted

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

Avatar

Avatar

kautuk_sahni

Community Manager

Total Posts

5.5K

Likes

955

Correct Answer

1.1K

Avatar

kautuk_sahni

Community Manager

Total Posts

5.5K

Likes

955

Correct Answer

1.1K
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