Hi,
I would like to write test cases for sling delegation pattern. I did find a few articles which pointed me in the right direction but I'm still unable to get the core components properties. Could anyone help in writing a minimal test case for a sling delegation model? Below is my code.
https://adapt.to/2021/schedule/whats-new-in-aem-mocks
https://github.com/adobe/aem-core-wcm-components/issues/1777
@ExtendWith(AemContextExtension.class)
public class CustomTextFormTest{
private final AemContext context = new AemContextBuilder(ResourceResolverType.JCR_MOCK)
.plugin(CORE_COMPONENTS)
.build();
private CustomTextForm customTextForm;
@BeforeEach
void setUp() {
context.create().resource("/apps/myproject/components/content/coreform/text",
"sling:resourceSuperType", "core/wcm/components/form/text/v2/text");
Page page = context.create().page("/content/test-page");
context.currentResource(context.create().resource(page, "extendedText",
"sling:resourceType", "myproject/components/content/coreform/text",
"required", "true",
"requiredMessage","Required message when required checkbox is true"
));
customTextForm= context.request().adaptTo(FormTextModel.class);
}
@Test
void testGetRequiredMessageWhenMsgIsAuthoredAndRequiredCheckBoxIsTrue() {
String expected = "Required message when required checkbox is true";
String actual = customTextForm.getRequiredMessage();
assertEquals(expected, actual);
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
HI @JeevanRaj
I have not tried this core component plugin, so not sure if this will work or not.
What issue are you geeting?
Hi @arunpatidar
Thanks for your reply. I had read through this earlier. However, I was looking for the approach that is provided in wcm.io library. It is addressed here (detailed video here). I'm trying to write a minimal test case for an extended core component using this approach but unable to get it to work so far.
HI @JeevanRaj
I have not tried this core component plugin, so not sure if this will work or not.
What issue are you geeting?
Below is the sling model. "text" object is null here when running the test case.
Sling Model:
@Model(adaptables = SlingHttpServletRequest.class, adapters = {Text.class, FormTextModel.class},
resourceType = "myproject/components/content/coreform/text",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class FormTextModel implements Text {
@Self
@Via(type = ResourceSuperType.class)
@Delegate(excludes = DelegationExclusion.class)
private Text text;
@Override
public String getRequiredMessage() {
return text.isRequired() && StringUtils.isBlank(text.getRequiredMessage()) ? MyProjectConstants.REQUIRED_MESSAGE : text.getRequiredMessage();
}
private interface DelegationExclusion { // Here we define the methods we want to override
String getRequiredMessage();
}
}
Test class:
@ExtendWith(AemContextExtension.class)
public class FormTextModelTest {
private final AemContext context = new AemContextBuilder(ResourceResolverType.JCR_MOCK)
.plugin(CORE_COMPONENTS)
.build();
private FormTextModel formTextModel;
@BeforeEach
void setUp() {
context.create().resource("/apps/myproject/components/content/coreform/text",
"sling:resourceSuperType", "core/wcm/components/form/text/v2/text");
Page page = context.create().page("/content/test-page");
Resource resource = context.create().resource(page, "extendedText",
PROPERTY_RESOURCE_TYPE, "mrproject/components/content/coreform/text",
"sling:resourceSuperType", "core/wcm/components/form/text/v2/text",
"required", "true",
"requiredMessage","Required message when required checkbox is true"
);
context.currentResource(resource);
formTextModel = context.request().adaptTo(FormTextModel.class);
}
@Test
void testGetRequiredMessageWhenMsgIsAuthoredAndRequiredCheckBoxIsTrue() {
assertNotNull(formTextModel, "Unable instantiate title component");
String expected = "Required message when required checkbox is true";
String actual = formTextModel.getRequiredMessage();
assertEquals(expected, actual);
}
}
Views
Replies
Total Likes
could you please check https://levelup.gitconnected.com/aem-extend-core-component-models-using-resource-type-association-an...
Views
Replies
Total Likes
Views
Likes
Replies