AEM Mocks: How to test Core Component Extensions
See also related adaptTo() Talk on this topic: What’s new in AEM Mocks.
Overview
Core Components Models can be extended using the Delegation Pattern:
Part of component definition:
sling:resourceSuperType="core/wcm/components/title/v2/title"
Model class example:
@Model(adaptables = SlingHttpServletRequest.class,
adapters = Title.class,
resourceType = "myproject/components/customTitle")
public class CustomTitle implements Title {
@Self @Via(type = ResourceSuperType.class)
private Title delegate;
}
How to write an Unit Test
Prerequisites:
Add the bundle
com.adobe.cq:core.wcm.components.core
as test dependency (the AEM API JARs contain only the interfaces, not the model implementation classes)Add the AEM WCM Core Components Plugin for AEM Mocks, see also List of AEM Mock Context Plugins
The component definition of your resource with the
sling:resourceSuperType
property needs to exist in the mocked repository of your unit test (see below)
Consider the following resource type hierarchy of your custom component:
myproject/components/customTitle
↪ core/wcm/components/title/v2/title
Prepare you unit test to reflect this hierarchy - either by creating a minimal component definition on the fly defining only the sling:resourceSuperType
property, or alternatively by loading the actual component definition from your project:
@BeforeEach
void setUp() {
context.create().resource("/apps/myproject/components/customTitle",
"sling:resourceSuperType", "core/wcm/components/title/v2/title");
/* alternative:
context.load().fileVaultXml("../ui.apps/src/main/content/jcr_root/apps/myproject/components/customTitle/.content.xml",
"/apps/myproject/components/customTitle"); */
page = context.create().page(SITE_ROOT_PATH);
}
Then you can write your test method with creating a resource and making it the current resource in the request: