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
BedrockMission!

Learn More

View all

Sign in to view all badges

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

Avatar

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,127 likes

Total Posts

6,121 posts

Correct Reply

1,144 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,127 likes

Total Posts

6,121 posts

Correct Reply

1,144 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile
kautuk_sahni
Community Manager

19-04-2021

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.

AEM AEMEBlogSeeding Experience Manager