How to write junit for below accordion multifield 6.5 | Community
Skip to main content
santhosh_kumark
Level 6
October 16, 2020
Solved

How to write junit for below accordion multifield 6.5

  • October 16, 2020
  • 1 reply
  • 1279 views

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
@586265
@7392697
public Resource accordion;


}

 

Regards,

Santosh

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 Sady_Rifat

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...

1 reply

Sady_Rifat
Community Advisor
Sady_RifatCommunity AdvisorAccepted solution
Community Advisor
October 16, 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...

kautuk_sahni
Community Manager
Community Manager
October 19, 2020
Wonderful reply.
Kautuk Sahni