Need help with JUnit on Sling Model | Community
Skip to main content
Rohan_Garg
Community Advisor
Community Advisor
July 25, 2022
Solved

Need help with JUnit on Sling Model

  • July 25, 2022
  • 1 reply
  • 1076 views

Hi AEM Community,

 

I have a sling model with an init method defined in @postconstruct as below - 

 

@PostConstruct
protected void init() {
Resource res = resourceResolver.getResource(currentPagePath);
System.out.println("Resource in init "+res);
Page currentPage = res.adaptTo(Page.class);
Resource imgRes = currentPage.getContentResource("image");

if(imgRes !=null){
ValueMap resourceProperties = imgRes.getValueMap();
thumbnailURL = resourceProperties.get(Constants.FILEREFERENCE, "");

ValueMap properties = currentPage.getProperties();
altTextThumbnail = properties.get(Constants.ALTTEXTTHUMBNAIL, "");
}
}

 

As you can see for the current page path, we are trying to get couple of properties form the page properties.

 

I have been trying to write the JUnit Test case for the same below unsuccessfully - 

I am trying to use Mockito framework to create mock resources, pages and value maps with the below content json file - 

{
"test-cmp": {
"jcr:primaryType": "cq:Page",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"jcr:title": "dev test",
"cq:template": "/conf/brands/settings/wcm/templates/home-page",
"sling:resourceType": "brands/components/page",
"altTextThumbnail": "Homepage-alt-text",
"root": {
"jcr:primaryType": "nt:unstructured",
"layout": "responsiveGrid",
"sling:resourceType": "brands/components/container",
"container": {
"jcr:primaryType": "nt:unstructured",
"layout": "responsiveGrid",
"sling:resourceType": "brands/components/container",
"navigation": {
"jcr:primaryType": "nt:unstructured",
"jcr:createdBy": "admin",
"childListingIcon": "icon-1",
"isMegamenu": "true",
"jcr:lastModifiedBy": "admin",
"jcr:created": "Fri Jul 22 2022 10:52:49 GMT+0530",
"disableShadowing": "false",
"navigationRoot": "/content/xyz/us/en",
"jcr:lastModified": "Mon Jul 25 2022 11:29:02 GMT+0530",
"dropdownIconExpand": "icon-minus",
"sling:resourceType": "platform/components/content/navigation/v1/navigation",
"collectAllPages": "true",
"structureStart": "1",
"dropdownIconCollapse": "icon-plus"
}
}
},
"image": {
"jcr:primaryType": "nt:unstructured",
"fileReference": "/content/dam/destinationpet/us/en/general/abc.png"
}
}
}
}

 

Can you please help with a pseudo code or the logic on how to approach this ?

@BeforeEach
public void setUp() throws Exception {
ctx.addModelsForClasses(CustomNavigationImpl.class);
ctx.load().json(TEST_CONTENT_JSON, TestConstants.TEST_ROOT_PAGE);

pageResource = ctx.resourceResolver().getResource("/content/test-cmp/jcr:content");

ctx.currentPage(pageResource.adaptTo(Page.class));

//Mockito.lenient().when(currentPagePath).thenReturn("/content/test-cmp/jcr:content");
//Mockito.lenient().when(resourceResolver.getResource(pageResource.getPath())).thenReturn(mockResource);
Mockito.lenient().when(resourceResolver.getResource(currentPagePath)).thenReturn(mockResource);

Mockito.lenient().when(mockResource.adaptTo(Page.class)).thenReturn(page);
Mockito.lenient().when(page.getContentResource("image")).thenReturn(imagemockResource);
Mockito.lenient().when(imagemockResource.getValueMap()).thenReturn(vM);
Mockito.lenient().when(vM.get(Constants.FILEREFERENCE, "")).thenReturn(file);

Mockito.lenient().when(page.getProperties()).thenReturn(vMImage);
Mockito.lenient().when(vMImage.get(Constants.ALTTEXTTHUMBNAIL, "")).thenReturn(altTextThumbnail);
}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

I assume that you are using AEM Mocks. 

 

In this case you don't need Mockito, but rather you can work like this:

 

@BeforeEach
public void setUp() throws Exception {
  ctx.addModelsForClasses(CustomNavigationImpl.class);
  ctx.load().json(TEST_CONTENT_JSON, TestConstants.TEST_ROOT_PAGE);
}

@Test
public void test_1 {
  MyModel myModel = context.resourceResolver.getResource(TestConstants.TEST_ROOT_PAGE).adaptTo(MyModel.class;
  assertNotNull(myModel);
  // .... 
}


There is no need to mock Resources anymore.

 

 

 

 

 

1 reply

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
July 25, 2022

I assume that you are using AEM Mocks. 

 

In this case you don't need Mockito, but rather you can work like this:

 

@BeforeEach
public void setUp() throws Exception {
  ctx.addModelsForClasses(CustomNavigationImpl.class);
  ctx.load().json(TEST_CONTENT_JSON, TestConstants.TEST_ROOT_PAGE);
}

@Test
public void test_1 {
  MyModel myModel = context.resourceResolver.getResource(TestConstants.TEST_ROOT_PAGE).adaptTo(MyModel.class;
  assertNotNull(myModel);
  // .... 
}


There is no need to mock Resources anymore.

 

 

 

 

 

Rohan_Garg
Community Advisor
Community Advisor
July 26, 2022

Thank you @joerghoh, it worked using only AEM Mocks.