Hello everyone
Does anyone knows how can I test (unit testing) this method in aem
public ComponentData getData() {
Resource investorListEventResource = this.request.getResource();
// Use ComponentUtils to verify if the DataLayer is enabled
if (ComponentUtils.isDataLayerEnabled(investorListEventResource)) {
return DataLayerBuilder.forComponent()
.withId(() -> investorListEventResource.getName() + "-" + getUniqueId())
.withTitle(() -> investorListEventResource.getPath())
.withDescription(() -> getAssociatedDocsDialogDescription())
.withLinkUrl(() -> currentPage.getPath() + "." + request.getRequestPathInfo().getExtension())
.build();
}
// return null if the Data Layer is not enabled
return null;
}
public ComponentData getData() {
Resource milestoneResource = this.resource;
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String dateFormatted = format1.format(getDate().getTime());
// Use ComponentUtils to verify if the DataLayer is enabled
if (ComponentUtils.isDataLayerEnabled(milestoneResource)) {
//if the data is empty or null the json won't generate the keys
return DataLayerBuilder.forComponent()
.withId(() -> milestoneResource.getName() )
.withType(() -> getType())
.withTitle(() -> getTitle())
.withDescription(() -> getCategory())
.withText(() -> dateFormatted)
.build();
}
// return null if the Data Layer is not enabled
return null;
}
Thanks in advance.
Solved! Go to Solution.
Views
Replies
Total Likes
Can you post your problem (along with a minimal failing test case) in the github project of the WCM Core Components [1]? I think the people can help better.
Use SlingMocks and create different versions of the resource, which you then can feed into this getData method.
I wrote a bit about it: https://cqdump.joerghoh.de/2019/01/17/writing-unittests-for-aem-part-3-mocking-resources/
[ERROR] testInvestorEventMaster Time elapsed: 0.041 s <<< ERROR!
java.lang.NoClassDefFoundError: com/adobe/cq/wcm/core/components/internal/models/v1/datalayer/builder/DataLayerSupplierImpl
...
Caused by: java.lang.ClassNotFoundException: com.adobe.cq.wcm.core.components.internal.models.v1.datalayer.builder.DataLayerSupplierImpl
Can you post your problem (along with a minimal failing test case) in the github project of the WCM Core Components [1]? I think the people can help better.