AEM mocks returns Null for @Reference objects (RequestResponseFactory requestResponseFactory ) | Community
Skip to main content
Level 2
October 12, 2020

AEM mocks returns Null for @Reference objects (RequestResponseFactory requestResponseFactory )

  • October 12, 2020
  • 3 replies
  • 5216 views

I am trying to mock RequestResponseFactory in my test class but I am getting null pointer exception.


Code:

String html = "";

HttpServletRequest request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);
WCMMode.DISABLED.toRequest(request);
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpServletResponse response = requestResponseFactory.createResponse(out);

try {
requestProcessor.processRequest(request, response, (org.apache.sling.api.resource.ResourceResolver) resolver);
response.getWriter().flush();
html = out.toString(UTF_8);
} catch (Exception e) {
}


Test File:

HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
RequestResponseFactory requestResponseFactory = mock(RequestResponseFactory.class);
PrivateAccessor.setField(servlet, "requestResponseFactory", requestResponseFactory);
String html = "";
when(requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path)).thenReturn(request);
request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);
WCMMode.DISABLED.toRequest(context.request());
ByteArrayOutputStream out = new ByteArrayOutputStream();
when(requestResponseFactory.createResponse(out)).thenReturn(response);
response = requestResponseFactory.createResponse(out);
try {
requestProcessor.processRequest(request, response,
(org.apache.sling.api.resource.ResourceResolver) resolver);
response.getWriter().flush();
html = out.toString(UTF_8);
} catch (Exception e) {
}

Suggest me how to mock @3214626 objects in mockito.

Note: I am using Junit5 and mokito and AEM 6.5.

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

3 replies

Level 4
October 12, 2020

Hi @sharma_naman3 

Try using the AemContext to mock the @Referenceobject run the tests using the same AemContext.

Please refer to the doc for Aemcontext examples.

 

https://wcm.io/testing/aem-mock/usage.html

 

Level 2
October 12, 2020
I am doing it with AemContext only to mock the @Referenceobjec still getting the same issue
Manjunath_K
Level 7
October 12, 2020

Hi @sharma_naman3 

I am not getting any null pointer exception when i tried executing same code in my local & i was able to get RequestResponseFactory mock object(refer below screenshot where test method executed without any exception). can you please mention in which line of code you are getting null pointer exception here or are you getting exception at this line of code "mock(RequestResponseFactory.class);" ?  it will be helpful if you provide more details to understand/reproduce this issue.

 

 

-Manjunath

Level 2
October 13, 2020

Hi Manjunath ..


Im getting null pointer in the main servlet "getContent" method.
//java.lang.NullPointerException
HttpServletRequest request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);


Test File


@2785667
void doGet(AemContext context) throws ServletException, IOException, Exception {
MockSlingHttpServletRequest request = context.request();
MockSlingHttpServletResponse response = context.response();
String html = getContent(damPath, (ResourceResolver) request.getResourceResolver());
servlet.doGet(request,response);}


private String getContent(String path, ResourceResolver resolver) throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
RequestResponseFactory requestResponseFactory = mock(RequestResponseFactory.class);

String html = "";
when(requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path)).thenReturn(request);
request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);
WCMMode.DISABLED.toRequest(context.request());
ByteArrayOutputStream out = new ByteArrayOutputStream();
when(requestResponseFactory.createResponse(out)).thenReturn(response);
response = requestResponseFactory.createResponse(out);
try {
requestProcessor.processRequest(request, response,
(org.apache.sling.api.resource.ResourceResolver) resolver);
response.getWriter().flush();
html = out.toString(UTF_8);
} catch (Exception e) {e.printStackTrace();
}}

 

 

April 8, 2022

Hi @sharma_naman3 ,

 

Did you find the solution? I need too.