Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

Getting started with Junit Code coverage in AEM using Mockito Framework | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

Getting started with Junit Code coverage in AEM using Mockito Framework by aemsimplifiedbynikhil

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

Getting started with Junit Code coverage in AEM using Mockito Framework

Q&A

Please use this thread to ask the related questions.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies

The ultimate experience is back.

Join us in Vegas to build skills, learn from the world's top brands, and be inspired.

Register Now