Unit Testing Hands on - For Sling Servlet | AEM Community Blog Seeding | Community
Skip to main content
kautuk_sahni
Community Manager
Community Manager
July 29, 2020

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

  • July 29, 2020
  • 0 replies
  • 916 views

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.

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