Unit test componentData | Community
Skip to main content
Level 2
February 18, 2022
Solved

Unit test componentData

  • February 18, 2022
  • 1 reply
  • 2403 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

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.

 

 

[1] https://github.com/adobe/aem-core-wcm-components

1 reply

joerghoh
Adobe Employee
Adobe Employee
February 23, 2022

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/

odabioAuthor
Level 2
February 28, 2022

[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

 

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
March 4, 2022

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.

 

 

[1] https://github.com/adobe/aem-core-wcm-components