I am trying to write the test case for AccordionSchemaModel it has a adaptable with SlingHttpServlet . When I create the test the init() method is called but the request is null and test case fails. Any suggestions ??
Solved! Go to Solution.
Views
Replies
Total Likes
Hello @SatishM1 -
While you are trying to write the Test Cases for the AccordianSchemaModel which implements the Adaptable interface and relies on SlingHttpServletRequest in its init() method.
Have you been mocking the SlingHttpServletRequest the right way? If not, please try below approach to create the mock object of SlingHttpServletRequest :
// Create a mock of SlingHttpServletRequest
SlingHttpServletRequest request = Mockito.mock(SlingHttpServletRequest.class);
// Set up necessary properties or behaviors
// For example, if your init() method accesses request parameters, you can mock the behavior as follows:
Mockito.when(request.getParameter("paramName")).thenReturn("paramValue");
// Pass the mock request object to the AccordionSchemaModel instance
AccordionSchemaModel model = new AccordionSchemaModel();
model.init(request);
// Proceed with your test assertions
// ...
Hope this helps
Hello @SatishM1 -
While you are trying to write the Test Cases for the AccordianSchemaModel which implements the Adaptable interface and relies on SlingHttpServletRequest in its init() method.
Have you been mocking the SlingHttpServletRequest the right way? If not, please try below approach to create the mock object of SlingHttpServletRequest :
// Create a mock of SlingHttpServletRequest
SlingHttpServletRequest request = Mockito.mock(SlingHttpServletRequest.class);
// Set up necessary properties or behaviors
// For example, if your init() method accesses request parameters, you can mock the behavior as follows:
Mockito.when(request.getParameter("paramName")).thenReturn("paramValue");
// Pass the mock request object to the AccordionSchemaModel instance
AccordionSchemaModel model = new AccordionSchemaModel();
model.init(request);
// Proceed with your test assertions
// ...
Hope this helps
Hi @SatishM1 ,
If you are using AemContext in your Junit Test Class, you should try mocking SlingHttpServletRequest from context's request
MockSlingHttpServletRequest request;
request = context.request();
AccordionSchemaModel schemaModel = request.adaptTo(AccordionSchemaModel.class);
Hope this helps,
Krishna
Hi @SatishM1 ,
When the request is null, mostly its due to the request not being served.
you can follow the below blog for further debugging steps.
https://myaemlearnings.blogspot.com/2020/08/unit-testing-in-aem-debugging-issues-in.html
Thanks,
Sweta
can you share stacktrace of the error once?
Views
Likes
Replies
Views
Likes
Replies