Wednesday, 19 July 2023

Unit Test for Sling Model using JUnit

What is JUnit?

Java-specific unit testing framework JUnit was developed as an open-source project. It is used to create and execute repeated automated tests that assist programmers in finding and swiftly fixing flaws in their code.

Why do we need JUnit Framework?

  • The Java programming language has an open-source unit testing framework called JUnit.
  • It helps in ensuring that the code is error-free and that every component of the codebase is operating as intended.
  • JUnit integrates with other development tools such as Eclipse and Maven.
  • JUnit provides a structure for writing tests, including annotations to specify test methods, assertions for checking expected results, and rules for organizing and running tests.
  • It gives Code Coverage to complete class/lines.
Which Api we will be using in Sling Model unit Test Case?
  • io.wcm.testing.mock.aem.junit5.*
  • org.mockito.junit.jupiter.*
  • org.junit.jupiter.api.*
How to Write Junit Test case for Sling Model
  • Folded Structure which is important while writing Unit TestCase


  • Inside Core Module you will get src under src you will find two folder one is main and another is test.
Now let's see how to generate test case:
  • I am using Eclipse Ide (integrated development environment) editor to write java code
 

  • Inside test we have models folder, than click right click, than select New, than select other option. 
  • Click on Other Option than search Junit and Select JunitTestCase.



  • Write Name of class and add Test.java.
  • Now, you have a Skeleton of Test class.
  • Now you have to Add Aem Specific Libraries.
  • You have a annotation is
@ExtendWith({ AemContextExtension.class, MockitoExtension.class })
  • This Annotation allow you to use your own Api.
  • Now I have to use Aem related Api
AemContext aemContext = new AemContext();
What is the meaning of @BeforeEach?
  • This method will execute Before each method.
  • used when different test cases share the same logic. The method with the @BeforeEach annotation always runs before the execution of each test case.
  • This annotation is commonly used to develop necessary preconditions for each @Test method.

aemContext.addModelsForClasses(rotationImpl.class);
  • There is a method add Classes add Model for classes now, you have to mention your SlingModel.
  • For this aemContext this is our Model.
  • It means we will be Testing this Model.
Now we talked about resource or Component let's see how to create resource.
  • As I have already discussed we should have json which contain all those properties and from this we will convert that json to resource.
Now we can see how to create json
  • You can get it from Aem itself.
  • Go to that page where it is and copy this path and go to Sling Exporter than paste copy path and Add .infinity.json.



  • Whatever you will save in your Node all data are present in the form of json.







  • Inside test folder resource folder is present than click right button select New and than select File and give name whatever you want and Add .json.
  •  In created folder you copy the whole json here.


 

  • I save properties as the value of a key.

  • Now Load this Json 
aemContext.load().json("/rotation.json", "/component")
  • aemContext.load().json present in "/component" path.

aemContext.currentResource("/component/rotation");
  • It means the current resource means the component than what is the current resource /component.
  • Our Run case is passed.

The Coverage is 100%.
Note:-The Coverage should be more than 50%.





No comments:

Post a Comment

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...