Image in test is initialized to null. | Community
Skip to main content
Jacket97
Level 2
June 22, 2022
Solved

Image in test is initialized to null.

  • June 22, 2022
  • 3 replies
  • 1408 views

Hi everyone, I was trying to create a Junit test for my ImagePlus component, an image to which I added a getName. I initialized the test in setUp like so:

@ExtendWith({AemContextExtension.class, MockitoExtension.class}) class ImagePlusTest { private final AemContext ctx = new AemContext(); private ImagePlus image; @Mock private ModelFactory modelFactory; @BeforeEach void setup() { ctx.addModelsForClasses(ImagePlus.class); ctx.load().json("mypath/ImagePlusTest.json", "/content"); ctx.registerService(ModelFactory.class, modelFactory, Constants.SERVICE_RANKING, Integer.MAX_VALUE); ctx.currentResource("/content/image"); image = ctx.request().adaptTo(ImagePlus.class); }


I don't understand why the image is set to null in the adapter. I've read that it might be that the implementation doesn't support the target type, but what can I do in this case? How do I see which types are supported or not?

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

I solved it by myself, the problem was in how to initialize a resource via the referenced Json file. All the fields that the image needs to be inserted into the environment must be entered

3 replies

SantoshSai
Community Advisor
Community Advisor
June 23, 2022

Hi @jacket97 ,

Can you please check if your ImagePlus Sling Model has 

@Model(adaptables = Resource.class)

Regards,

Santosh 

Santosh Sai
Jacket97
Jacket97Author
Level 2
June 23, 2022

Hi Santosh, it has this 

(adaptables = SlingHttpServletRequest.class

 

Jacket97
Jacket97Author
Level 2
June 23, 2022

@jacket97 

@Model(adaptables = {SlingHttpServletRequest.class, Resource.class})

 Should work, try this and let us know.


Sorry, Nothing change.

 

B_Sravan
Community Advisor
Community Advisor
June 23, 2022

Hi @jacket97 ,

please add your Implementation class to your context,

 

ctx.addModelsForClasses(ImagePlus.class)

to ctx.addModelsForClasses(ImagePlusImpl.class).

Thanks,

Sravan 

Jacket97
Jacket97AuthorAccepted solution
Level 2
August 22, 2022

I solved it by myself, the problem was in how to initialize a resource via the referenced Json file. All the fields that the image needs to be inserted into the environment must be entered