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?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi @Julkar__Naim
Please check this https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/it-s-still-a-problem-how-t...
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
Views
Like
Replies