Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

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

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Level 6
2 Replies

Avatar

Correct answer by
Level 6

Avatar

Community Advisor

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!