Leiste mit Community-Erfolgen erweitern.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

Diese Konversation wurde aufgrund von Inaktivität geschlossen. Bitte erstellen Sie einen neuen Post.

GELÖST

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 Akzeptierte Lösung

Avatar

Korrekte Antwort von
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

Lösung in ursprünglichem Beitrag anzeigen

6 Antworten

Avatar

Community Advisor

Hi @Jacket97 ,

Can you please check if your ImagePlus Sling Model has 

@Model(adaptables = Resource.class)

Regards,

Santosh 


Santosh Sai

AEM BlogsLinkedIn


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.


Santosh Sai

AEM BlogsLinkedIn


Avatar

Level 2

Sorry, Nothing change.

 

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

Korrekte Antwort von
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