Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

aemey
Level 2
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
Vijayalakshmi_S
Correct answer by
Community Advisor
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
Vijayalakshmi_S
Community Advisor
Community Advisor

Hi @aemey 

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

aemey
Level 2
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

 

Vijayalakshmi_S
Correct answer by
Community Advisor
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