Expand my Community achievements bar.

How to write Unit Tests for Extended Core Components' Java Models


Level 4

I extended the Text Core Component's Java Class with a simple "moreText" variable. 


But I am still using some of the Core Component's methods in the HTL so I need them in my Java Class. 


How can I Unit Test the Extended Core Component's methods that require the ResourceSuperType?


package com.adobe.aem.guides.wknd.core.models;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.wcm.core.components.models.Text;
import lombok.Getter;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.models.annotations.via.ResourceSuperType;

        adaptables = SlingHttpServletRequest.class,
        adapters = {ComponentExporter.class},
        resourceType = ExtendedCoreComponentModel.RESOURCE_TYPE,
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
        name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
        extensions = ExporterConstants.SLING_MODEL_EXTENSION
public class ExtendedCoreComponentModel implements Text {

    static final String RESOURCE_TYPE = "wknd/components/extendedcorecomponent";

    @Via(type = ResourceSuperType.class)
    protected Text text;

    @default(values = "Hello World.")
    private String moreText;

    public String getText() {
        return text.getText();

    public boolean isRichText() {
        return text.isRichText();


4 Replies

Thanks for the help but this is not a solution. I saw these answers you mention before posting this question. 


The first Q&A uses ...


 ... but this is a deprecated method and field. 


The second Q&A literally mentions they cannot test overridden methods. 


The code you share in your repo also avoids unit testing an overridden method (or a ResourceSuperType). 


Community Advisor

Hello @jeremylanssiers ,

You can follow this article where each step is described: https://medium.com/@sadyrifat/aem-unit-test-case-for-sling-delegation-pattern-1f011b947fb3 

Another GitHub commit link and repo for check your maven dependency also: https://github.com/Sady-Rifat/aem-demo/commit/81ce47fc28b70d31bee0034e2cadf69b721d892e 

Hope this will help you.


Level 4

I have not been able to test your solution yet. However I was able to test a ResourceSuperType by directly injecting a mock value. E.g.



CoreComponent coreComponent;


ExtendedCoreComponent extendedCoreComponent = new ExtendedCoreComponent();


extendedCoreComponent.coreComponent =coreComponent;


The obvious drawback is I had to make that coreComponent protected instead of private in my ExtendedCoreComponent class.