Friday, November 6, 2020

Creating JUnit Tests in AEM

 

Creating JUnit Tests in AEM

 

Unit tests are build-time tests written in Java that verify expected behavior of Java code.

 

Following are the benefits of writing Junit Tests in AEM:


·        It finds logical bugs early in the code, which makes our code more reliable.

 

·        JUnit is useful for developers, who work in a test-driven environment as it forces them to develop more readable, reliable and bug-free code which builds confidence during development.

 

 

·        It increases the productivity of the programmer and the stability of program code, which in turn reduces the stress on the programmer and the time spent on debugging.

 

Each unit tests are typically small, and validate the outcome of methods (or units of work) against expected results.

 

Below Maven dependencies are required to support writing and running the tests:

 

·        JUnit4

·        Mockito Testing Framework

·        Apache Sling Mocks

·        wcm.io Test Framework

 

The first three i.e. JUnit4, Mockito and Sling Mocks dependencies are automatically added to the project during project setup through Maven

But io.wcm Testing Framework dependency must be added to the project's root pom.xml.

<dependency>

        <groupId>io.wcm</groupId>

        <artifactId>io.wcm.testing.aem-mock.junit4</artifactId>

        <version>2.3.2</version>

        <scope>test</scope>

</dependency> 

 

Creating Junit Tests in Eclipse

 

We can do this in Eclipse, by right-clicking on the Java class to test, and selecting the New > Other > Java > JUnit > JUnit Test Case.

 

Below are the steps to create the JUnit test type is New JUnit 4 Test:



Verify the following while creating the Junit Tests:

·        The JUnit Maven dependencies set up in our pom.xml's.

·        The package is the java package of the class being tested (BylineImpl.java)

·        The Source folder points to the core project, which instructs Eclipse where the unit test files are stored.

·        The setUp() method stub will be created manually; we'll see how this is used later.

 

Click Next - The next step helps with the auto-generation of test methods.

Typically each public method of the Java class has at least one corresponding test method, validating its behavior.

Often a unit test will have multiple test methods testing a single public method, each representing a different set of inputs or states.



Run the JUnit Test Case by right-clicking on the class name, and Run As > JUnit Test.

If the test is green it indicated by green color status bar. Otherwise, it’ll be shown in red color. Also, we will get the code coverage details in Coverage tab.



Below is the sample code to for user details component in my-page:

 

import org.mockito.runners.MockitoJUnitRunner;

 

@RunWith(MockitoJUnitRunner.class)

public class UserDetailsTest {

 

            private UserDetails userDetails;

            private Resource resource;

            private final String myPagePath = “/content/we-retail/us/en/my-page”;

 

            @Rule

            public final SlingContext context = new SlingContext(ResourceResolverType.JCR_MOCK);

 

            @Before

            public void setUp() throws Exception {           

                        context.load().json(“/content.json”, myPagePath);

                        context.addModelsForPackage(“<mention-your-package-here>”);

            }

 

            @Test

            public void getFirstNameTest() throws Exception {

                        resource = context.resourceResolver()

                                                .getResource(myPagePath + “/jcr:content/root/responsivegrid/userdetails”);

                        userDetails = resource.adaptTo(UserDetails.class); 

                        assertEquals(userDetails.getFirstName(), “Lavanya”);

            }          

 

            @Test

            public void getLastNameTest() throws Exception {

                        resource = context.resourceResolver()

                                                .getResource(myPagePath + “/jcr:content/root/responsivegrid/userdetails”);

                        userDetails = resource.adaptTo(UserDetails.class); 

                        assertEquals(userDetails.getLastName(), “Malyala”);

            }

 

}

 

References:

https://helpx.adobe.com/gr_en/experience-manager/kt/sites/using/getting-started-wknd-tutorial-develop/part8.html

https://aem.adobemarketingclub.com/junit-in-aem/

No comments:

Post a Comment