Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Unable to adapt resource to my slingModel in test

Avatar

Level 2

Hey , i have created json and i am trying to adapt resource to my sling model class. I have created the json correctly using .json on the browser with all fields of component dialog populated , however when i give resource path till the component my adapted sling model returns null . If i update the resource path to anything inside the component in json , i get an adapted sling model with null values for the fields inside (which is expected since the path is wrong).

My issue here is why is not adapting when i give correct path till the component in json . Is there something that i might be missing?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
7 Replies

Avatar

Level 5

You can debug your Junit to see why the Sling Model has not been adopted. Check if there is any logic inside the @Postconstruct method of your Sling Model.

Avatar

Level 2

There is logic within the post construct actually, so what do i have to do in this regard? The post construct method is retrieving a list of of child items present in dialog multifield and assigning to an array

Avatar

Correct answer by
Community Advisor

Avatar

Community Advisor

@Zendarkke : Can you please share the code snippet of how you are resolving the resource. Also, if  possible the resource json.

Avatar

Community Advisor

Hi,

The reason this is failing is that the "resource" (loaded through your JSON) does not meet all the requirements expected by your Sling model. You should check the following aspects of your Sling model:

  • Is it adaptable from a Resource or SlingServlet? How you are getting your slingModel in your test?
  • Is it tied to a resourceType?
  • Does it have all the required fields?
  • Does it depend on other OSGi services or objects that may not be available?
  • Etc.

A good way to figure out what is going on is to use the SlingModelFactory to create the Sling model instead of adapting it from the resource. This approach will provide a more informative error message about what failed during the model creation. Something like this:

MyModel model = context.getService(ModelFactory.class).createModel(request, MyModel.class);

 

Hope this helps.



Esteban Bustamante

Avatar

Administrator

@Zendarkke Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni