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;
@Model(
adaptables = SlingHttpServletRequest.class,
adapters = {ComponentExporter.class},
resourceType = ExtendedCoreComponentModel.RESOURCE_TYPE,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@Exporter(
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class ExtendedCoreComponentModel implements Text {
static final String RESOURCE_TYPE = "wknd/components/extendedcorecomponent";
@Self
@Via(type = ResourceSuperType.class)
protected Text text;
@getter
@ValueMapValue
@default(values = "Hello World.")
private String moreText;
@Override
public String getText() {
return text.getText();
}
@Override
public boolean isRichText() {
return text.isRichText();
}
}
Views
Replies
Total Likes
@jeremylanssiers Please check out this post on how you can do null check for
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-test-cases-for-sling...
Also the example here
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CustomTeaser.java
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CustomTeaserTest.java
hope this helps!
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 ...
FieldSetter.setField()
... 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).
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.
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.
@Mock
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.