Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to write junit for below accordion multifield 6.5

Avatar

Community Advisor

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

1 Accepted Solution

Avatar

Correct answer by
Level 2

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

View solution in original post

1 Reply

Avatar

Correct answer by
Level 2

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