How to write junit for below accordion multifield 6.5

santhosh_kumark

16-10-2020

Can any one help me with junit for the multifield model class for accordion:

 

@Model(adaptables = Resource.class)
public class Accordion {

// Inject the accordion node under the current node
@inject
@Optional
public Resource accordion;


}

 

Regards,

Santosh

Accepted Solutions (1)

Accepted Solutions (1)

Sady_Rifat

16-10-2020

Hi, unit test for Accordion will be like this->

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
public class AccordionTest {
private final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
private Accordion accordion;

@Mock
private ModelFactory modelFactory;

@BeforeEach
public void setUp() throws Exception {
context.addModelsForClasses(Accordion.class);
context.load().json("/com/java/components/accordion.json", "/content/accordion");
context.currentResource("/content/accordion");
context.registerService(ModelFactory.class, modelFactory, Constants.SERVICE_RANKING, Integer.MAX_VALUE);
accordion = context.currentResource().adaptTo(Accordion.class);
}

@Test
public void accordionModelTest() throws Exception {
assertNotNull(accordion);
}
}

 In the resources, accordion.json should exist /com/java/components under this directory. accordion.json will contain json format of crx content  node or:

{
"items": {
"item1": {
"title": "Career",
"link": "demo link"
},
"item2": {
"title": "Profession",
"link": "another demo link"
}
}
}

Hope it will help you...

Answers (0)