Abstract
Welcome back AEM folks! In this article we will look into the process by which we can write junit test cases for the code coverage for our Java code in AEM. As this is the first article which will just cover the basics, if required I can go ahead and publish more article on in-depth knowledge on Junit.
So to cover the code using Junit test cases we will use Mockito framework. So, lets go ahead and add the below dependency to our POM.xml
org.mockito
mockito-core
1.10.19
test
Now once we are done adding the maven dependency, lets have a Java class for our article. So below is my Accordion Java Sling Model class.
package com.company.models;
import javax.inject.Inject;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
/**
* @author Nikhil Kumar
*
*/
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Accordion {
@Inject
private String name;
@Inject
private String heading;
@Inject
private String description;
public String getName() {
return name;
}
public String getHeading() {
return heading;
}
public String getDescription() {
return description;
}
}
Below is my Test class where I am using before and test method to set some values to my Java class and then later testing out the values that I set earlier.
Read Full Blog
Q&A
Please use this thread to ask the related questions.
Kautuk Sahni