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.
- 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
- 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.
- 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.
- 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
- 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.
- It means the current resource means the component than what is the current resource /component.
No comments:
Post a Comment