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
Views
Replies
Total Likes
Hi,
You can try like below, where I read multifield and return a list of adapted type. In case you need to do it nestedly
Java
Test
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);
}
}
Hi,
How did you adapted the class. check if you are using delegation pattern
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CustomTeaser.java
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CustomTeaserTest.java
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();
Hi Arun,
Thanks for your support, I am able to resolve my issue.
Regards,
Shlok
@sthakur Can you please share the solution with the community for posterity?
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies