Expand my Community achievements bar.

SOLVED

MockSlingHttpServletRequest how to set a selector

Avatar

Level 2

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

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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/w... 



Arun Patidar

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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/w... 



Arun Patidar

Avatar

Community Advisor

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