Hi,
I have a servlet uses SlingServletPaths and Get Method. Servlet receives more than one request parameter then perform operations.
I have below ServletTest to test my ExampleServlet. Below Error is thrown when I ran mvn clean install command
java.lang.NullPointerException: Cannot invoke "org.apache.sling.api.resource.Resource.getPath()" because "parent" is null
@ExtendWith(AemContextExtension.class)
public class ExampleServletTest {
ExampleServlet example = new ExampleServlet();
void testDoGet(AemContext context)throws ServletException,IOException {
MockSlingHttpServletRequest request = context.request();
MockSlingHttpServletResponse response = context.response();
request.setQueryString("param1=test1");
request.setQueryString("param2=test2");
request.setQueryString("param3=test3");
example.doGet(request, response);
}
}
Any pointer or help would be appreciated?
Solved! Go to Solution.
Views
Replies
Total Likes
"parent is null" in NPE refers to the first argument on the create method which is nothing but the parent resource under which we are trying to create a resource.
Resource resource = resolver.create(resolver.getResource(folderPath), folderName,
folderProperties);
Cross check that the AemContext object in Test class is aware/set to the parent resource.
Thanks for the response.
I have extended my ExampleServlet to add backend call. This is hard to share here.
But code is failed through Unit test here. I am sending folderPath, folderName from ExampleServletTest and folderProperties added through HashMap
final Map<String, Object> folderProperties = new HashMap<>();
folderProperties.put(JcrConstants.JCR_PRIMARYTYPE,JcrResourceConstants.NT_SLING_FOLDER);
folderProperties.put(JcrConstants.JCR_TITLE, folderTitle);
Resource resource = resolver.create(resolver.getResource(folderPath), folderName,
folderProperties);
Above code works fine If I call through Standard Servlet using component
"parent is null" in NPE refers to the first argument on the create method which is nothing but the parent resource under which we are trying to create a resource.
Resource resource = resolver.create(resolver.getResource(folderPath), folderName,
folderProperties);
Cross check that the AemContext object in Test class is aware/set to the parent resource.
Views
Likes
Replies