Expand my Community achievements bar.

AEM Mockito Junit testing for nested dailog box or multifield

Avatar

Level 2

I am new to implement AEM Mockito testing and want to write junit test cases for nested dailog box for an example We have one model class "AccordionListModel" which contain the list of "AccordionModel"

While writing Junit not getting model class reference (AccordionListModel.java) via adaptTo method

Below are the attached files for reference:

 

-AccordionListModel.java

 

sthakur_1-1654579301946.png

 

-AccordionModel.java

sthakur_2-1654579391921.png

 

- AccordionListModel.json

sthakur_3-1654579429711.png

AccordionListModelTest.java

sthakur_4-1654579541000.png

 

 

7 Replies

Avatar

Level 2

Hi Arun,

scenerio is different in my case I am using adaptTo()  method to create a model object which is not working if we have nested dailog box.

Below is my Test class code snnipet. adaptTo method throwing null pointer exception if model class has 

nested implemention.

class AccordionListModelTest {

public final AemContext ctx = new AemContext();

public AccordionListModel accordionListModel;

@BeforeEach
void setUp() throws Exception {
ctx.addModelsForClasses(AccordionListModel.class);
ctx.load().json("/com/corp/core/models/components/content/utilities/AccordionListModel.json", "/content");
}

@Test
void getTitle(){
ctx.currentResource("/content/AccordionList");
accordionListModel = ctx.request().adaptTo(AccordionListModel.class); // Not able to get the reference of AccordionListModel via adaptTo, getting null pointer exception
String expected = "hello";
String actual = accordionListModel.getTitle();
assertEquals(expected, actual);
}

}

 

Avatar

Level 2

Hi Arun,

 

Below is the snippet of my model class

@Model(
adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class LocationsModel extends ComponentModel implements Validatable {
private static final Logger log = LoggerFactory.getLogger(LocationsModel.class);
private static final String CSS_CLASS_NAME = "dover-locations";

@OSGiService
private GoogleMapAPIConfiguration mapAPIConfiguration;

@ValueMapValue
private String title;

@ValueMapValue
private int zoomLevel;

@ChildResourceFromRequest
private final List<LocationItem> locations = emptyList();

private String locationsJson;
private String key;

if you see the above code if we remove @ChildResourceFromRequest annotation then I am able to get the LocationsModel object via adaptTo() in Test class

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class LocationsModelTest {

public final AemContext ctx = new AemContext();

public LocationsModel locationsModel;

@BeforeEach
void setUp() throws Exception {
ctx.addModelsForClasses(LocationsModel.class);
ctx.load().json("/com/dovercorporation/core/models/components/content/utilities/LocationComponentTest.json", "/content");
}

@Test
void getTitle() {
ctx.currentResource("/content/location");
locationsModel = ctx.request().adaptTo(LocationsModel.class);
String expected = "dover-locations";
String actual = locationsModel.getTitle();
assertEquals(expected, actual);
}
}

Avatar

Level 2

I am facing issue if my model class has multifield functionality for an example

in above case my model class 

LocationsModel.java
public class LocationsModel extends ComponentModel implements Validatable {

@ChildResourceFromRequest
private final List<LocationItem> locations = emptyList();


Avatar

Level 2

Hi Arun,

 

Thanks for your support, I am able to resolve my issue.

 

Regards,

Shlok

Avatar

Administrator

@sthakur Can you please share the solution with the community for posterity?



Kautuk Sahni