Test case for AccordionSchemaModel is failing | Adobe Higher Education
Skip to main content
June 23, 2023
解決済み

Test case for AccordionSchemaModel is failing

  • June 23, 2023
  • 4 の返信
  • 1000 ビュー

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

 

このトピックへの返信は締め切られました。
ベストアンサー 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 の返信

Tanika02
Tanika02回答
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?