Junit Test for sling delegation pattern is not working
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
@2434638(type = ResourceSuperType.class)
@delegate-1(excludes = ComponentDelegateExclusion.class)
private com.adobe.cq.wcm.core.components.models.Image image;
@ValueMapValue
@1497330(booleanValues = false)
private boolean alignCenter;
@ValueMapValue
@1497330(booleanValues = false)
private boolean removeGap;
@ValueMapValue
@1497330(booleanValues = false)
private boolean fluidImage;
@9944223
public boolean getAlignCenter() {
return alignCenter;
}
@9944223
public boolean getRemoveGap() {
return removeGap;
}
@9944223
public boolean getFluidImage() {
return fluidImage;
}
@9944223
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);
}
@2785667
void getAlignCenter() {
assertEquals(jsonProperties.get("alignCenter"), Boolean.toString(image.getAlignCenter()));
}
@2785667
void getRemoveGap() {
assertEquals(jsonProperties.get("removeGap"), Boolean.toString(image.getRemoveGap()));
}
@2785667
void getFluidImage() {
assertEquals(jsonProperties.get("fluidImage"), Boolean.toString(image.getFluidImage()));
}
@2785667
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?