Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Image in test is initialized to null.

Avatar

Level 2

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?

1 Accepted Solution

Avatar

Correct answer by
Level 2

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

View solution in original post

6 Replies

Avatar

Community Advisor

Hi @Jacket97 ,

Can you please check if your ImagePlus Sling Model has 

@Model(adaptables = Resource.class)

Regards,

Santosh 

Avatar

Level 2

Hi Santosh, it has this 

(adaptables = SlingHttpServletRequest.class

 

Avatar

Community Advisor

@Jacket97 

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

 Should work, try this and let us know.

Avatar

Community Advisor

Hi @Jacket97 ,

please add your Implementation class to your context,

 

ctx.addModelsForClasses(ImagePlus.class)

to ctx.addModelsForClasses(ImagePlusImpl.class).

Thanks,

Sravan 

Avatar

Correct answer by
Level 2

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