MockSlingHttpServletRequest how to set a selector | Community
Skip to main content
Level 2
November 25, 2024
Solved

MockSlingHttpServletRequest how to set a selector

  • November 25, 2024
  • 2 replies
  • 740 views

Hi, 

I want to write a Testcase for my Servlet, which expects a specific selector.

So far I ve always created my MockSlingHttpServletRequest request object using AemContext context.request().

However, I have nt found any method which might allow me to set a selector for it ?

Would be great if you could share an example setting a selector.

thanks a lot for your support in advance.

 

--

Volker

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 arunpatidar

Hi @vhochsteintef 

context.requestPathInfo().setSelectorString(DownloadServlet.SELECTOR);

Please check

https://github.com/adobe/aem-core-wcm-components/blob/main/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/servlets/DownloadServletTest.java 

2 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
November 25, 2024
Ritesh_Mittal
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 25, 2024

Hi @vhochsteintef ,

 

To set a selector in a MockSlingHttpServletRequest for your AEM servlet test, you can use the setSelectorString() method. Here's a brief example:

 

MockSlingHttpServletRequest request = context.request();

request.setSelectorString("myselector");

// Your servlet logic here

servlet.doGet(request, context.response());

 

// Verify the selector

assertEquals("myselector", request.getRequestPathInfo().getSelectorString());

 

 

This will set the selector to "myselector" and you can then use it in your test.

 

Thanks

Ritesh Mittal