Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Unit test case for Sling delegation pattern

Avatar

Community Advisor

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://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-test-cases-for-sling...

 

https://adapt.to/2021/schedule/whats-new-in-aem-mocks

 

https://github.com/adobe/aem-core-wcm-components/issues/1777

 

https://github.com/adobe/aem-core-wcm-components/blob/main/testing/aem-mock-plugin/src/test/java/com...

 

@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);
}
}

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

HI @JeevanRaj 
I have not tried this core component plugin, so not sure if this will work or not.

What issue are you geeting?



Arun Patidar

View solution in original post

5 Replies

Avatar

Community Advisor

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.

Avatar

Correct answer by
Community Advisor

HI @JeevanRaj 
I have not tried this core component plugin, so not sure if this will work or not.

What issue are you geeting?



Arun Patidar

Avatar

Community Advisor

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);
}
}