No exporter named jackson supports java.lang.String | Community
Skip to main content
Level 2
January 27, 2022

No exporter named jackson supports java.lang.String

  • January 27, 2022
  • 3 replies
  • 2331 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

Level 3
January 27, 2022

Ankush, 

try below statement if it works

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

Level 2
January 28, 2022

Hi @nitinl 

This does not work. Getting the same issue. 

 

Regards,

Ankush Dhingra

Vijayalakshmi_S
Level 10
January 27, 2022

@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-develop/project-archetype/unit-testing.html?lang=en

 

 

Level 2
January 28, 2022

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

antoniom5495929
Level 7
January 28, 2022

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-develop/project-archetype/unit-testing.html?lang=en#

 

Thanks,

Antonio

joerghoh
Adobe Employee
Adobe Employee
January 28, 2022

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.