Expand my Community achievements bar.

SOLVED

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

Avatar

Level 1

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;

}

}

1 Accepted Solution

Avatar

Correct answer by
Level 7

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

View solution in original post

4 Replies

Avatar

Correct answer by
Level 7

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

Avatar

Level 10

See this GEMS Session here on testing: Adobe Experience Manager Help | Tools to use for testing Adobe Experience Manager applications

We are going to hold an in-depth Ask the AEM Community experts on this topic too in March. Reg will open early March.

The last place to look for more information is here:

Adobe Experience Manager Help | Getting Started with AEM Sites Chapter 8 - Unit Testing

Hope this helps...

Avatar

Level 1

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.