Expand my Community achievements bar.

No exporter named jackson supports java.lang.String

Avatar

Level 2

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

8 Replies

Avatar

Level 4

Ankush, 

try below statement if it works

jsonData = modelFactory.exportModelForResource(extendedComponentResource, "jackson", String.class,
Collections.<String, String> emptyMap());

Avatar

Level 2

Hi @NitinL 

This does not work. Getting the same issue. 

 

Regards,

Ankush Dhingra

Avatar

Community Advisor

@ankushd75501011 

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.

https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-wknd-tutorial-devel...

 

 

Avatar

Level 2

Hi @Vijayalakshmi_S 

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

Avatar

Level 7

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.

....

  • Provides a variety of common required mock objects and helpers such as SlingHttpServletRequest objects, a variety of mock Sling and AEM OSGi services such as ModelFactory, PageManager, Page, Template, ComponentManager, Component, TagManager, Tag, etc.
    • Note that not all methods for these objects are implemented!

[0] https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-wknd-tutorial-devel...

 

Thanks,

Antonio

Avatar

Community Advisor

@ankushd75501011 

Could you please share

  • the complete error trace when you execute the unit test. (Test failure exception trace)
  • Actual Model class and Test class file if possible. 

Avatar

Employee Advisor

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.