How to retrieve SlingHttpServletRequest from ResourceResolver



I would like to retrieve the request used to resolve the resources I retrieve from the ResourceResolver.

I am performing integration testing using Teleporter Rule And can resolve the resource but can't find its corresponding request.

@Rule public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "Launchpad");
@Test public void testPage() throws LoginException {
     ResourceResolverFactory resourceResolverFactory = teleporter.getService(ResourceResolverFactory.class);
     ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
     Resource resource = resourceResolver.getResource("/content/test/en");
     PageModel page = resource.adaptTo(PageModel.class);

I would like to be able to retrieve the request like I can with my unit tests using SlingContext.

SlingContextImpl slingContext = new SlingContextImpl(); 

I would like to retrieve the request like I can with my unit tests and properly test my sling models that inject the request.

Accepted Solutions (1)

Accepted Solutions (1)




I think that SlingModels, which only adapt from a Resource, are much more versatile than SlingModels which can only be adapted from a request. Because the adaption works in nearly every case, and not just in the context of a request. It also makes testing much easier.

You can "simulate" a request from javacode, see [1] for an example. It should also work in the context of your IntegrationTests.

[1] Get the rendered HTML for an AEM resource, component or page - Adobe Experience Manager | AEM/CQ | A...

Answers (3)

Answers (3)



Using the requestResponseFactory in conjunction with the request processor is definitely what I would like to do to. The issue is from that approach is that I seem unable to adapt the request to my model afterwards.



Hi Jorg,

Ok then, if that is the case. What would be the best way to stimulate a request for a server side integration test and receive a SlingHttpServletRequest from which I can retrieve the resource.