Unit test case for Sling delegation pattern | Community
Skip to main content
JeevanRaj
New Participant
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
New Participant
April 10, 2023
JeevanRaj
JeevanRajAuthor
New Participant
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.

arunpatidar
arunpatidarAccepted solution
New Participant
April 11, 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?

Arun Patidar