Skip to main content
New Participant
May 3, 2023

Getting NPE in a config value in Junit test case

  • May 3, 2023
  • 1 reply
  • 1016 views

I'm writing a Junit test case for a resourceType servlet. A configuration is referenced like below in the servlet.

 

@3214626
SessionMgmtConfigService config;
 
And during runtime, we are checking the below condition - 
if(!pagePath.contains(config.getProjectContentPath())) where pagePath is one of the query params.
 
Now, in the unit test for this, I'm using AemContext and have registered the configuration like - 
SessionMgmtConfigService config = new SessionMgmtConfigImpl();
aemContext.registerService(SessionMgmtConfigService.class, config);
servlet.config = config;
 
But, I'm getting a NPE while trying to debug this test case on the .contains line.
 
How can I get the values for the config params?
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

MayurSatav
New Participant
May 3, 2023

hi @goyalkritika,

 

Check that the implementation of SessionMgmtConfigImpl returns a valid value for getProjectContentPath(). In the case of this method returning null, the NPE would occur. but for better understanding could you please share complete error info here?

New Participant
May 3, 2023

Do I need to mock the config implementation as well?

 

Here is the snippet of the error - 

 

MayurSatav
New Participant
May 3, 2023

See i told you it is not returning valid value, i can see it is throwing null pointer. try mocking it