JUnit 5 throws java.lang.NullPointerException: Cannot invoke "org.apache.sling.api.resource.Resource.getPath()" because "parent" is null | Community
Skip to main content
Level 2
November 22, 2021
Solved

JUnit 5 throws java.lang.NullPointerException: Cannot invoke "org.apache.sling.api.resource.Resource.getPath()" because "parent" is null

  • November 22, 2021
  • 1 reply
  • 11945 views

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();

  @2785667

  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?

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

@aemey,

 "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. 

1 reply

Vijayalakshmi_S
Level 10
November 23, 2021

Hi @aemey 

Is it possible for you to share your actual Servlet class. In this case ExampleServlet

aemeyAuthor
Level 2
November 23, 2021

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

 

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
November 29, 2021

@aemey,

 "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.