Unit Testing Hands on - For Sling Servlet
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) Inst...