Need help in creating the Junit test cases for sling models, and sling servlets
Team,
Could you please help me in creating the Junit test classes for Sling models and Sling servlets, currently we are using AEM 6.3.2 version and trying to create the test cases but we are getting Null for Resource, ResouceResolver and Request objects.
Please find the below code base and suggest me what is best approach to create the Junit test class using Mockito.
@Model(adaptables = { SlingHttpServletRequest.class,
Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TestHeaderModel {
@SlingObject
private SlingHttpServletRequest slingRequest;
@Inject
@Via(MemberPortalDigitalConstants.RESOURCE)
ResourceResolver resourceResolver;
/** The dashboard path. */
@Inject
@Via(MemberPortalDigitalConstants.RESOURCE)
private static String dashboardPath;
/** The logo url. */
@Inject
@Via(MemberPortalDigitalConstants.RESOURCE)
private String logoUrl=StringUtils.EMPTY;
.
.
.
.
.
.
@PostConstruct
public void init() {
String currentPagePath = StringUtils.EMPTY;
if (slingRequest != null) {
currentPagePath = slingRequest.getPathInfo();
}
Page rootPage = getGlobalNavRootPage(resourceResolver);
if (rootPage != null) {
homePage = rootPage.getTitle();
}
try {
navList = getNavigationTree(rootPage);
} catch (Exception e) {
LOG.error("Exception occured while generating header navigation:::", e);
}
}
public static Page getGlobalNavRootPage(ResourceResolver resourceResolver) {
String navRootPage = StringUtils.EMPTY;
Page rootPage = null;
if (resourceResolver != null) {
if (StringUtils.isEmpty(navRootPage) && StringUtils.isNotEmpty(dashboardPath)) {
navRootPage = dashboardPath;
}
LOG.debug("Selected navRootPage is : " , navRootPage);
}
rootPage = CommonUtils.retrievePage(navRootPage, resourceResolver);
return rootPage;
}
private List<NavigationItems> getNavigationTree(final Page root) throws Exception {
LOG.debug("Inside getNavigationTree method of " , CLASS_NAME);
List<NavigationItems> navList = new ArrayList<NavigationItems>();
.
.
.
.
return navList;
}
}