내 커뮤니티 업적 표시줄을 확대합니다.

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

Junit Test for sling delegation pattern is not working

Avatar

Level 2

Hi, 

 

I am trying to write test for custom image component. Here is the model impl

 

@Model(
    adaptables = SlingHttpServletRequest.class,
    adapters = {Image.class, ComponentExporter.class},
    resourceType = ImageImpl.RESOURCE_TYPE,
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@Exporter(
    name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
    extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class ImageImpl implements Image {

    public static final String RESOURCE_TYPE = "myproject/components/content/image/v1/image";

    @Self
    @Via(type = ResourceSuperType.class)
    @Delegate(excludes = ComponentDelegateExclusion.class)
    private com.adobe.cq.wcm.core.components.models.Image image;

    @ValueMapValue
    @default(booleanValues = false)
    private boolean alignCenter;

    @ValueMapValue
    @default(booleanValues = false)
    private boolean removeGap;

    @ValueMapValue
    @default(booleanValues = false)
    private boolean fluidImage;

    @Override
    public boolean getAlignCenter() {
        return alignCenter;
    }

    @Override
    public boolean getRemoveGap() {
        return removeGap;
    }

    @Override
    public boolean getFluidImage() {
        return fluidImage;
    }

    @Override
    public String getExportedType() {
        return RESOURCE_TYPE;

 

And this is my test class

 

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class ImageImplTest {

    private final AemContext context = new AemContextBuilder().plugin(CORE_COMPONENTS)
                                                              .build();
    private static final String TEST_BASE = "/components/image";

    private ValueMap jsonProperties;

    private Image image;
    @BeforeEach
    void setUp() {
        context.addModelsForClasses(com.adobe.cq.wcm.core.components.models.Image.class);
        context.addModelsForClasses(ImageImpl.class);
        context.load().json(TEST_BASE + "/image-comp.json", "/apps/" + ImageImpl.RESOURCE_TYPE);
        Resource imageResource = context.load().json(TEST_BASE + "/image.json", "/content/image");
        jsonProperties = imageResource.getValueMap();
        context.currentResource("/content/image");
        image = context.request().adaptTo(Image.class);
    }

    @test
    void getAlignCenter() {
        assertEquals(jsonProperties.get("alignCenter"), Boolean.toString(image.getAlignCenter()));
    }

    @test
    void getRemoveGap() {
        assertEquals(jsonProperties.get("removeGap"), Boolean.toString(image.getRemoveGap()));
    }

    @test
    void getFluidImage() {
        assertEquals(jsonProperties.get("fluidImage"), Boolean.toString(image.getFluidImage()));
    }

    @test
    void testResourceType() {
        assertEquals(jsonProperties.get("sling:resourceType"), image.getExportedType());
    }

}

 

The tests are working fine for custom properties I have added, but the  private com.adobe.cq.wcm.core.components.models.Image image is always null. I can't access any core components property. image-comp.json is the infinity.json of /apps/myproject/components/content/image/v1/image and image.json is the infinity.json from /content/***/image

How can I solve this?

주제

토픽은 커뮤니티 콘텐츠를 분류하여 관련성 있는 콘텐츠를 찾는 데 도움이 됩니다.

1 채택된 해결책 개

Avatar

정확한 답변 작성자:
Community Advisor
2 답변 개

Avatar

정확한 답변 작성자:
Community Advisor

Avatar

Community Advisor

Hi @Julkar__Naim ,

Try to write getter public function for all the core component private function and then try to access it in your JUNIT class. 
Also make sure you have these property mentioned in JSON resource file.

Thanks
Tarun