Test case for AccordionSchemaModel is failing | Community
Skip to main content
June 23, 2023
Solved

Test case for AccordionSchemaModel is failing

  • June 23, 2023
  • 4 replies
  • 1000 views

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

 

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

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 🙂

4 replies

Tanika02
Tanika02Accepted solution
Level 7
June 23, 2023

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 🙂

krishna_sai
Community Advisor
Community Advisor
June 23, 2023

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

Level 4
June 23, 2023

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

Umesh_Thakur
Community Advisor
Community Advisor
June 23, 2023

can you share stacktrace of the error once?