Skip to main content
Level 3
November 23, 2022
Solved

Need to write test case for dialogue list items in sling model

Hi Team,

 

Hope you are doing well 

 

Can anyone please help to write test case for this function

public List<Map<String, String>> getSliderDetail() {
List<Map<String, String>> sliderDetailsMap=new ArrayList<>();

try {
Resource sliderDetail = resource.getChild("sliderDetails");
if(sliderDetail!=null){
for (Resource slider : sliderDetail.getChildren()) {
Map<String,String> sliderMap=new HashMap<>();
sliderMap.put("title",slider.getValueMap().get("title",String.class));
sliderMap.put("description",slider.getValueMap().get("description",String.class));
sliderDetailsMap.add(sliderMap);
}
}
}catch (Exception e){
LOG.info("\n Model Error : {} ",e.getMessage());
}
return sliderDetailsMap;
}

private final AemContext context = AppAemContext.newAemContext();
@Test
void getSliderDetail() {
Resource resource = context.resourceResolver().getResource(JCR_CONTENT+"demo-aem/components/Customslider");
Resource sliderDetail = resource.getChild("sliderDetails");
assertNotNull(resource);
assertNotNull(sliderDetail);
}


I have tried with this but don't know how can I pass 

Resource resource = context.resourceResolver().getResource(JCR_CONTENT+"demo-aem/components/Customslider");
In test cases 

any link or suggestion please
Thanks

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

2 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
November 23, 2022

Hi @bhagchand ,

   I would configure a page with component(ex:Customslider) whose sling model you are testing and get the json of page with ".infinity.json" extension and use that JSON(ex: modelToTest.json) as the resource in the junit test code. Once you have the JSON uploaded into "bundle/src/test/resources" folder , you can pass it to context

 

private AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

context.load().json("modelToTest.json", "/content");
context.currentResource("/content/page1/Customslider");

Make sure you have "sliderDetails" object list in the JSON by configuring it in the page. 

 

Hope that helps!

Translate