Posts

Showing posts from July, 2020

Unit Testing Hands on - For Sling Servlet

Image
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) Instantiate Servlet to be te

Unit Testing Hands on - For WCMUsePojo

Image
This post illustrate the unit test for WCMUsePojo , Java class for backend logic as part of  AEM Component development. Quick Recap about WCMUsePojo :   WCMUsePojo initializes the objects associated with Bindings (Eg: WCMBindings/SlingBindings) via its init(Bindings) method which in turn calls activate() method for post initialization tasks. This init(Bindings bindings) method gets called if the POJO is instantiated in HTL via data-sly-use attribute. Bindings(javax.script.Bindings)  extends  Map(java.util.Map)  and hence it is basically a map object where  key  (type String) is the global variable and  value  is the respective object.  Once when the bindings are initialized, we are able to make use of the global variables which is available to us via methods like  getCurrentPage(), getProperties() etc. Example : When we call getCurrentPage() to get page object, below happens behind the scene  (considering the above flow) getCurrentPage() -> bindings.get(WCMBindings.CURRENT_PAGE

Unit Testing in AEM - Hands on

Image
This post is about hands-on on Unit Testing Java class, part of an AEM application. We will be using AEM Mocks from  io.wcm.testing.mock.aem.junit5.*  and Mockito framework -  org.mockito.* (reason for using this is explained in previous post ) JUnit Version : JUnit 5 AEM Maven archetype : 22 IDE : Eclipse Testing related maven dependency : (available by default in AEM Maven archetype) JUnit5 (org.junit) AEM mocks (io.wcm.testing.aem-mock.junit5) Mockito framework (org.mockito) We mostly follow "Implementation first development" approach - Desired functionality is first coded and then test case is written for the same. For creating Test Java class,  Create Test Java Class Create AemContext Implement setUp method (creating test fixture, annotated with @BeforeEach ) Implement methods to be tested. (annotated with @Test ) Create Test Java Class: In Eclipse IDE, right click on the Java class to be tested ->  New -> Other -> JUnit -> JUnit Test case We will have opti