Need help in creating the Junit test cases for sling models, and sling servlets

jampannat110919 05-02-2019

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;

}

}

Mark Solution

Are these answers useful?
Help other community members by marking useful answers as accepted.

Accepted Solutions (0)

Answers (4)

Answers (4)

jampannat110919 06-02-2019

Hi Antonio,

Is there any problem with using the @PostConstruct annotation for init() method in my model, and all my models were already developed by using the @PostConstruct annotation to init() method.

If I mock the Resource, resolver and so on, those are returning null when I call underTest.init() method from my TestInit() method as shown below.

underTest = new TestHeaderModel();

underTest.init();

And can you please let me know how to call the init() method to execute the logic inside this method.

antoniom5495929 06-02-2019

Hi,

i've used Mockito a lot of time ago.

In my case i just only apply the annotation @Mock on top of the element that i need to mock.

@Mock

Resource resource;

@Mock

Node node;

@Mock

NodeIterator nodeItr;

@Mock

Session session;

public void setUp() throws Exception {

  MockitoAnnotations.initMocks(this);

}

Probably you need to identify the method that you want to test and than mock the element that you need.

By reading your model, i don't find a lot of element to test just because your method have a very large purpose.

Let me know if you need some specific details.

Thanks,

Antonio