I have to get the json export for a sling model in another sling model. The code that works is:
String exportedJson = modelFactory.exportModelForResource(resource, "jackson",
String.class, new HashMap<String, String>());
This works when the code actually executes, but when I am trying to write a junit for this, I get the error, No exporter named jackson supports java.lang.String
I decompiled the jar, and saw that String class is supported, but I am guessing the JacksonExporter is not getting binded at the time of running the test.
AemContext provides a mock implementation of the ModelFactory service
So, am I missing something ?
Regards,
Ankush Dhingra
Views
Replies
Total Likes
Ankush,
try below statement if it works
jsonData = modelFactory.exportModelForResource(extendedComponentResource, "jackson", String.class,
Collections.<String, String> emptyMap());
We need to mock the ModelFactory and provide dummy implementation for the method - exportModelForResource() using Mockito's when and thenReturn in your test class and then assert accordingly.
Please find below sample snippet(BylineImplTest) from official docs (where the dummy implementation is for getModelFromWrappedRequest of ModelFactory) Along the similar lines, you can write one.
Thanks for the reply. But AemContext provides an OOTB object of the ModelFactory. So, why is there a need to mock it separately ?
Regards,
Ankush Dhingra
Hi @ankushd75501011 ,
cause as you can see from the documentation [0], even if the ModelFactory is provided as ootb object, not all the methods are mocked, so in this case you need to proceed by creating a mock for your specific method.
....
Thanks,
Antonio
Could you please share
.
Hi,
In your case I would mock this call completely and return a static result, because you don't want to test the SlingModelExporter, but just your own code. Returning a static test string is the best way to handle this.