AEM Mockito Junit testing for nested dailog box or multifield | Community
Skip to main content
June 7, 2022

AEM Mockito Junit testing for nested dailog box or multifield

  • June 7, 2022
  • 1 reply
  • 2160 views

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

 

 

-AccordionModel.java

 

- AccordionListModel.json

AccordionListModelTest.java

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

sthakur-1Author
June 7, 2022

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);
}

}

 

sthakur-1Author
June 8, 2022

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);
}
}

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();