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

Avatar

Avatar
Level 1
Sharma_Naman3
Level 1

Likes

0 likes

Total Posts

4 posts

Correct reply

0 solutions
View profile

Avatar
Level 1
Sharma_Naman3
Level 1

Likes

0 likes

Total Posts

4 posts

Correct reply

0 solutions
View profile
Sharma_Naman3
Level 1

11-10-2020

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 @reference objects in mockito.

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

Accepted Solutions (0)

Answers (2)

Answers (2)

Avatar

Avatar
Contributor
Level 2
VeenaK
Level 2

Likes

18 likes

Total Posts

15 posts

Correct reply

3 solutions
Top badges earned
Contributor
Shape 1
Give Back
Boost 5
Boost 3
View profile

Avatar
Contributor
Level 2
VeenaK
Level 2

Likes

18 likes

Total Posts

15 posts

Correct reply

3 solutions
Top badges earned
Contributor
Shape 1
Give Back
Boost 5
Boost 3
View profile
VeenaK
Level 2

12-10-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

 

Avatar

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
Manjunath_K
MVP

12-10-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.

 

junt2.png

 

-Manjunath

Sharma_Naman3

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


@test
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();
}}

 

 

Manjunath_K

@Sharma_Naman3 

Are you getting exception at this line of code in getContent() method? if possible share exception stack trace. 

 

request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);

Sharma_Naman3

Yes,I am getting null pointer exception at beolow line only in main servlet.

 

HttpServletRequest request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);


Logs
java.lang.NullPointerException
at de.test.coe.aem.core.servlets.testServlet.getHtmlContent(testServlet.java:93)
at de.test.coe.aem.core.servlets.testServlet.doGet(testServlet.java:79)
at de.test.coe.aem.core.servlets.testServletTest.doGet(testServletTest.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)

Manjunath_K

@Sharma_Naman3 

As per the above exception stack trace which you have shared, its clear that there is no null pointer exception thrown when you mock RequestResponseFactory. null pointer exception is thrown while executing servlet.doGet(request,response); at line 93 in testServlet.getHtmlContent() method. since you have not shared getHtmlContent() method here, please cross check what code you have at this line number which resulting null pointer exception.