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
Views
Replies
Total Likes
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
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
Okay which adaption have you used for sling model . Can you please adapt both slinghttpservlet and resource object.
@Sanjay_Bangar Currently it is using slinghttpservlet only. I will check with both and update on the same.
@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));
Try @SlingObject or @OsgiService annotation for QueryBuilder instead of adapting it from resource resolver then try mocking QueryBuilder in test case
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
Hi Ravi,
Null pointer is when running the test case or in sling model itself?
Can you try something like below
@Mock
QueryBuilder mockQueryBuilder;
when(mockResourceResolver.adaptTo(QueryBuilder.class)).thenReturn(mockQueryBuilder);
Hi @Ravi_Joshi,
Please find below thread where I had a chance to share sample test class for Query Builder logic in Sling Servlet.
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.
Views
Likes
Replies
Views
Likes
Replies
Views
Like
Replies