unable to mock Sling model object which is annotated with a custom annotation
Hi everyone ,
I am facing issue in mocking a sling model object which have custom annotation.
this is my model class for which i am writing test class : -
public class DownloadLinkModel {
@586265
private AuthorizationService authorizationService;
@586265
private Resource resource;
@586265 @2434638("resource")
private String listType;
@586265 @2434638("resource")
private String parentPath;
@586265 @2434638("resource")
private String[] tags;
@586265 @2434638("resource")
private String openNewWindow;
@586265 @2434638("resource")
private int maxLimitToDisplay;
@586265 @2434638("resource")
private String enableTeaserContent;
@586265 @2434638("resource")
private String sortBy;
@586265 @2434638("resource")
private int maxLimitInEachCol;
@586265 @2434638("resource")
private String downloadLinksheader;
@586265 @2434638("resource")
private Resource download;
@586265 @2434638("resource")
private String enableInnerGrid;
@586265
private Page resourcePage;
@586265 @ScriptVariable
private Page currentPage;
@586265 @2434638("resource")
private Resource res;
@586265 @3200416("sling-object")
private SlingHttpServletRequest slingRequest;
@586265 @3200416("sling-object")
private ResourceResolver resourceResolver;
@MySiteConfigInjector
private Optional<SiteSlingModel> siteSlingModel; // NULL
private boolean isDateFormat = false;
@PostConstruct
protected void init() {
if (siteSlingModel.isPresent()) { // getting null pointer exception here
isDateFormat = Boolean.parseBoolean(siteSlingModel.get().getDateFormat());
}
if (null != resourcePage){
switch(getListType()) {
// BUSINESS LOGIC
}
}
}
class DownloadLinkModelTest {
private static final String TEST_PATH = "/content/en/page/jcr:content/root/responsivegrid";
public final AemContext context = new AemContext();
private DownloadLinkModel downloadLinkModel;
@Mock
private Resource resource;
@Mock
private SiteSlingModel siteSlingModel;
@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
context.addModelsForClasses(DownloadLinkModel.class);
context.create().page("/content/en");
Page page = context.create().page("/content/en/page");
context.currentPage(page);
context.load().json("downloadlink.json", "/resource");
resource = context.currentResource("/resource/downloadlink-tags");
context.request().setResource(resource);
downloadLinkModel = context.request().adaptTo(DownloadLinkModel.class); // NULL
}
@2785667
@15595559("Test getDownloadLink - Tags List")
public void testGetDownloadLinkTagsList() {
assertNotNull(downloadLinkModel); // test failed
assertEquals("tags", downloadLinkModel.getListType());
assertEquals("title", downloadLinkModel.getSortBy());
}
"downloadlink-tags": {
"jcr:primaryType": "nt:unstructured",
"jcr:createdBy": "b994430",
"jcr:lastModifiedBy": "a047776",
"sortBy": "title",
"openNewWindow": "true",
"jcr:created": "Thu May 11 2023 06:18:03 GMT-0400",
"tags": [
"tag1"
],
"jcr:lastModified": "Wed Sep 13 2023 01:48:04 GMT-0400",
"sling:resourceType": "myProject/components/general/download-link",
"listType": "tags",
"maxLimitToDisplay": "40"
}

