Expand my Community achievements bar.

SOLVED

Unit test componentData

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

3 Replies

Avatar

Employee Advisor

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/

Avatar

Level 3

[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

 

Avatar

Correct answer by
Employee Advisor

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