Expand my Community achievements bar.

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
@Deleted Account
public Resource accordion;


}

 

Regards,

Santosh

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

2 Replies

Avatar

Correct answer by
Community Advisor

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