Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 2

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

  @test

  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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @aemey 

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

Avatar

Level 2

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

 

Avatar

Correct answer by
Community Advisor

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