Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

JUnit Test Cases for Sling Models based on Delegation Pattern - Failing

Avatar

Level 2

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!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @hiralvasa ,

I think the problem is in your @Via tag, Just remove this tag & this should work as your expectation.

@Delegate(types = com.adobe.cq.wcm.core.components.models.Title.class, excludes = Handled.class)
@Self
private com.adobe.cq.wcm.core.components.models.Title delegate;

Check this commit where I tried to generate your scenario, https://github.com/Sady-Rifat/aem-demo/commit/81ce47fc28b70d31bee0034e2cadf69b721d892e 

 

Since your actual model class is not shared I put this as a standard and a typical pattern.

I hope you will get help and your problem will be solved.

View solution in original post

6 Replies

Avatar

Level 4

@hiralvasa It seems that Externalizer is not mocked. Try below code if it works:

@Mock
private Externalizer externalizer;

 Also, in the @BeforeEach method add below line:

context.registerService(Externalizer.class, externalizer);

 

Regards,

Ayush

Avatar

Level 2

Hi @ayush-804 ,

 

Thanks for the quick response. Tried adding the mock to Externalizer, unfortunately, still didn't work.
Attaching the details error log : 

java.lang.RuntimeException: After setup failed (com.adobe.cq.wcm.core.components.testing.mock.ContextPlugins$1@dd4aec3): Unable to inject mandatory reference 'externalizer' for class com.adobe.cq.wcm.core.components.internal.link.DefaultPathProcessor : no matching services were found.

	at org.apache.sling.testing.mock.osgi.context.ContextPlugins.executeAfterSetUpCallback(ContextPlugins.java:218)
	at io.wcm.testing.mock.aem.junit5.AemContextExtension.lambda$beforeEach$1(AemContextExtension.java:129)
	at io.wcm.testing.mock.aem.junit5.AemContextExtension.applyAemContext(AemContextExtension.java:180)
	at io.wcm.testing.mock.aem.junit5.AemContextExtension.beforeEach(AemContextExtension.java:127)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachCallbacks$2(TestMethodTestDescriptor.java:163)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$6(TestMethodTestDescriptor.java:199)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:199)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachCallbacks(TestMethodTestDescriptor.java:162)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:129)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
	at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
	at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
	Suppressed: java.lang.NullPointerException
		at org.mockito.junit.jupiter.MockitoExtension.afterEach(MockitoExtension.java:184)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachCallbacks$12(TestMethodTestDescriptor.java:257)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:273)
		at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$14(TestMethodTestDescriptor.java:273)
		at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:272)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachCallbacks(TestMethodTestDescriptor.java:256)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:141)
		... 47 more
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.
	at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.injectServiceReference(OsgiServiceUtil.java:416)
	at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.injectServices(OsgiServiceUtil.java:399)
	at org.apache.sling.testing.mock.osgi.MockOsgi.injectServices(MockOsgi.java:163)
	at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerInjectActivateService(OsgiContextImpl.java:158)
	at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerInjectActivateService(OsgiContextImpl.java:146)
	at com.adobe.cq.wcm.core.components.testing.mock.ContextPlugins.setUp(ContextPlugins.java:52)
	at com.adobe.cq.wcm.core.components.testing.mock.ContextPlugins$1.afterSetUp(ContextPlugins.java:41)
	at com.adobe.cq.wcm.core.components.testing.mock.ContextPlugins$1.afterSetUp(ContextPlugins.java:38)
	at org.apache.sling.testing.mock.osgi.context.ContextPlugins.executeAfterSetUpCallback(ContextPlugins.java:215)
	... 56 more

Avatar

Community Advisor

@hiralvasa ,

 

Try

@Mock
private Externalizer externalizer;

In the @BeforeEach method

context.registerAdapter(ResourceResolver.class, Externalizer.class, externalizer);

 

Hope this helps.

Avatar

Level 2

No luck, I'm still getting same error.
Thanks @Sudheer_Sundalam for the quick reply.

 

Also, is it mentioned anywhere about mocking the Externalizer class while writing JUnits for extended sling models of core components?

 

Avatar

Community Advisor

Hi @hiralvasa 

 

Can you please provide Model Code snippet and you can remove sensitive code, of-course?

Avatar

Correct answer by
Community Advisor

Hello @hiralvasa ,

I think the problem is in your @Via tag, Just remove this tag & this should work as your expectation.

@Delegate(types = com.adobe.cq.wcm.core.components.models.Title.class, excludes = Handled.class)
@Self
private com.adobe.cq.wcm.core.components.models.Title delegate;

Check this commit where I tried to generate your scenario, https://github.com/Sady-Rifat/aem-demo/commit/81ce47fc28b70d31bee0034e2cadf69b721d892e 

 

Since your actual model class is not shared I put this as a standard and a typical pattern.

I hope you will get help and your problem will be solved.