Unit test case for Sling delegation pattern | Community
Skip to main content
JeevanRaj
Community Advisor
Community Advisor
April 6, 2023
Solved

Unit test case for Sling delegation pattern

  • April 6, 2023
  • 1 reply
  • 1917 views

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-models-based-on-delegation-pattern/m-p/437458/highlight/true#M124890

 

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/adobe/cq/wcm/core/components/testing/mock/CoreComponentsMockTest.java

 

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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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

What issue are you geeting?

1 reply

arunpatidar
Community Advisor
Community Advisor
April 10, 2023
JeevanRaj
Community Advisor
JeevanRajCommunity AdvisorAuthor
Community Advisor
April 11, 2023

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.

JeevanRaj
Community Advisor
JeevanRajCommunity AdvisorAuthor
Community Advisor
April 12, 2023

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