Hi all, I'm having trouble with this line in my unit test returning null. I can't figure out why the adaptTo is returning null. Let me know if you have any idea as to what I may be doing wrong. Thank you!
removed
I've tried to include the relevant code below.
My class
removed
And the relevant code from the unit test:
removed
Solved! Go to Solution.
Views
Replies
Total Likes
@Dusty1186 adaptTo(...) returns null if anything fails in the Model class without showing any error. You can try using ModelFactory.createModel(...) instead of adaptTo(...) for SlingModels which would log all the error details and would help you in debugging the code further.
ReferenceModel referenceModel = context.getService(ModelFactory.class).createModel(request, ReferenceModel.class);
Hope this helps.
Regards,
Ayush
Doesn’t you should either work with resource.adaptTo() or resourceresolver.adapto() instead of request.adaptTo() ?
please check this https://wcm.io/testing/aem-mock/usage.html
@Dusty1186 adaptTo(...) returns null if anything fails in the Model class without showing any error. You can try using ModelFactory.createModel(...) instead of adaptTo(...) for SlingModels which would log all the error details and would help you in debugging the code further.
ReferenceModel referenceModel = context.getService(ModelFactory.class).createModel(request, ReferenceModel.class);
Hope this helps.
Regards,
Ayush
This was it. I discovered that in the init of my model class there was a property that would return null in the unit test, as I was assigning a value to it after the adaptTo. This would cause the adaptTo in the unit test to fail, even though everything as far as the model goes was working correctly in the application.
Thanks.
Hi @Dusty1186
The CONTENT_PATH where you are loading your json has to be absolute path and not relative. I think json is not loading properly.
Please make CONTENT_PATH absolute and try.
Views
Like
Replies
Views
Likes
Replies
Views
Likes
Replies