JUnit Test Cases for Sling Models based on Delegation Pattern - Failing
Hi Everyone,
I followed all the steps mentioned in AEM Mock plugin for Core Component : https://github.com/adobe/aem-core-wcm-components/issues/1777
I am trying to write JUnits for the extended model of Core title component here and getting Error :
Caused by: org.apache.sling.testing.mock.osgi.ReferenceViolationException: Unable to inject mandatory reference 'externalizer' for class com.adobe.cq.wcm.core.components.internal.link.DefaultPathProcessor : no matching services were found.
Below is the code for the test class and related classes.
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class TitleImplTest {
public static final String COMPONENT_PATH = "component_path";
public static final String COMPONENT_CONTENT_PATH = "content_path";
public final static String PAGE_PATH = "page_path";
private final AemContext ctx = AppAemContext.newAemContextForCore();
private Title title;
/**
* Setup aem context with resource and models.
*/
@BeforeEach
void setUp() {
// Load all sling models to AEM context
ctx.addModelsForClasses(Title.class);
// Load Page resource with component to context
ctx.load().json("/models/TitleImplTest/TitleImplTest.json", PAGE_PATH);
// Load Component to context for delegation pattern
ctx.load().json("/models/TitleImplTest/Component.json", COMPONENT_PATH);
// Set Current path to component under page
ctx.currentResource(COMPONENT_CONTENT_PATH);
// Adapt request to Sling Model
title = ctx.request().adaptTo(Title.class);
}
/**
* Test Title Impl methods.
*/
@Test
void testTitleImpl() {
// Validate all fields/methods
assertEquals("Explore Process Optimization", title.getText(), "Verify title text");
assertFalse(title.enableAnchor(), "Verify anchor disabled");
assertEquals("explore-process-optimization", title.getAnchorID(), "Verify anchor id");
assertEquals("/jcr:content/root/responsivegrid/container/title", title.getMeta().getComponentRelativePath(), "Verify component relative path");
}
public static AemContext newAemContextForCore() {
return new AemContextBuilder().resourceResolverType(ResourceResolverType.JCR_MOCK)
.plugin(CORE_COMPONENTS).build();
}
@Delegate(types = com.adobe.cq.wcm.core.components.models.Title.class, excludes = Handled.class)
@Self @Via(type = ResourceSuperType.class)
private com.adobe.cq.wcm.core.components.models.Title delegate;
Could anyone please help here to fix the above issue. Thanks in advance!
