Expand my Community achievements bar.

resource resolver mock in test not able to adapt query builder

Avatar

Level 5

Hi ,

 

I am facing one issue with sling model junit mock. I have created an resource resolver mock in the test file and in the model we are adapting resource resolver to query builder. while accessing through test case it is returning null value.

 

Model code snippet :

resourceResolver.adaptTo(QueryBuilder.class)

 

Resource resolver mock is created through AEM context object only.

 

Let me know your thoughts on the same.

 

Thanks,

Ravi Joshi

 

10 Replies

Avatar

Community Advisor

Hi @ravi_joshi ,

                         In sling model which annotation have used for resourceresolver I mean @self  annotations.if it is @self annotation then it will gives you null pointer. You need to add @via resources then it will be not giving any error.

 

Kr,

Sanjay

 

Kr,

Sanjay

Avatar

Level 5

Hi @Sanjay_Bangar ,

 

Thanks for replying. We are using @Inject annotation for resource resolver injection. I tried using Via annotation as well but no luck. Let me know if you have any other thought on the same.

 

Thanks,

Ravi Joshi

Avatar

Community Advisor

Okay which adaption have you used for sling model . Can you please adapt  both slinghttpservlet and resource object.

 

 

Avatar

Level 5

@Sanjay_Bangar  Currently it is using slinghttpservlet only. I will check with both and update on the same.

Avatar

Level 5

@Sanjay_Bangar  Adding resource resolved the null pointer exception for the query builder adaption but now jcr session throwing null pointer for the below code snippet.

/*Working now*/
QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
/*Throwing nullpointer error after adapting session*/
Query query = queryBuilder.createQuery(PredicateGroup.create(map),resourceResolver.adaptTo(Session.class));

Avatar

Level 7

Try @SlingObject or @OsgiService annotation for QueryBuilder instead of adapting it from resource resolver then try mocking QueryBuilder in test case

Avatar

Level 5

Hi @Dinu_Arya ,

 

I have tested with both the Injections but still the same. Giving null for querybuilder when using @SlingObject or @OsgiService.

 

Thanks,

Ravi Joshi

Avatar

Level 7

Hi Ravi,

 

Null pointer is when running the test case or in sling model itself?

Avatar

Community Advisor

Can you try something like below

 

@Mock
QueryBuilder mockQueryBuilder;

when
(mockResourceResolver.adaptTo(QueryBuilder.class)).thenReturn(mockQueryBuilder);

 



Arun Patidar

Avatar

Community Advisor

Hi @ravi_joshi,

Please find below thread where I had a chance to share sample test class for Query Builder logic in Sling Servlet. 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-servlet-junit-writing-...

 

You can use the mock implementation (in @BeforeEach method) in your Sling Model Test case and continue per your Sling Model logic. 

If you face any issues, please do share your actual Sling Model class if possible.