Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Getting NPE in a config value in Junit test case

Avatar

Level 5

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

 

@reference
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?
3 Replies

Avatar

Community Advisor

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?

Avatar

Level 5

Do I need to mock the config implementation as well?

 

Here is the snippet of the error - 

goyalkritika_0-1683091663461.png

 

Avatar

Community Advisor

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