Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Test case for AccordionSchemaModel is failing

Avatar

Level 1

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 ??

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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

Avatar

Community Advisor

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

Avatar

Level 4

Hi @SatishM1 ,

 

When the request is null, mostly its due to the request not being served.

  • Have you created the resource and the json is validated.
  • check the mocking object.
  • Try to debug by adding debug points and find the exact point of the request failing.

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

Avatar

Community Advisor

can you share stacktrace of the error once?