December 28, 2020
Estimated Post Reading Time ~

Writing AEM WCMUsePojo JUnit Test Example

This article will demonstrate how to write AEM Unit tests for building WCMUsePojo Java classes using the Junit4 testing framework. With developers being more visual, the source code is posted below.

In this example, I am building a Sightly utils class that helps me append .html to page paths that are configured by the author. The utils class can be used by any AEM components during development. A value is passed through a parameter in this demo.

Technologies here used are:
  • AEM project archetype 19 (link)
This example uses the AEM project archetype 19 to generate a new AEM project, Junit 4 will be used as the testing framework.

Sightly Example Use:
<!-- somecomponent -->
<div class="somecomponent">
    <a href="${lnk.link}" target="_blank" rel="noopener noreferrer">
        Click Here
    </div>
</div>


WCMUsePojo Example Code:

package com.sourcedcode.core.wcm.utils;

import com.adobe.cq.sightly.WCMUsePojo;
import org.apache.commons.lang3.StringUtils;

public class LinkChecker extends WCMUsePojo {

private static final String LINK = "link";

private String link;

@Override
public void activate() {
resolveLink();
}

/**
* Resolves the path of a JCR page path with a .html extension
*/
private void resolveLink() {
link = get(LINK, String.class);
if (StringUtils.isNotEmpty(link)) {
if (!link.contains(".html")) {
link += ".html";
}
}
}

public String getLink() {
return link;
}

}


WCMUsePojo Unit Test Example Code:

package com.sourcedcode.core.wcm.utils;

import org.junit.Test;

import javax.script.Bindings;

import static junitx.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class LinkCheckerTest {

@Test
public void testLinkValueWithNoHTML() {
Bindings bindings = mock(Bindings.class);
when(bindings.get("link")).thenReturn("/content/sourcedcode/home");
LinkChecker use = new LinkChecker();
use.init(bindings);
assertEquals("/content/sourcedcode/home.html", use.getLink());
}

@Test
public void testLinkValueWithHTML() {
Bindings bindings = mock(Bindings.class);
when(bindings.get("link")).thenReturn("/content/sourcedcode/home.html");
LinkChecker use = new LinkChecker();
use.init(bindings);
assertEquals("/content/sourcedcode/home.html", use.getLink());
}

}


Tip:
I hope the example above helps you out with your development of Java Classes that extend the WCMUsePojo Class. Instead of writing a new WCMUsePojo Class, you can utilize magic that sightly 1.4 offers.

Sightly 1.4 offers a quicker way for you to add extensions to a given url, as examples below:

${'sourcedcode/home' @ extension='html'}
<!-- outputs: sourcedcode/home.html -->

${'sourcedcode/home.json' @ extension='html'}
<!-- outputs: sourcedcode/home.html -->

${'sourcedcode/home.selector.json' @ extension='html'}
<!-- outputs: sourcedcode/home.selector.html -->

${'sourcedcode/home.json/suffix' @ extension='html'}
<!-- outputs: sourcedcode/home.html/suffix -->

${'sourcedcode/home.json?key=value' @ extension='html'}
<!-- outputs: sourcedcode/home.html?key=value -->

${'sourcedcode/home.json#fragment' @ extension='html'}
<!-- outputs: sourcedcode/home.html#fragment -->

${'sourcedcode/home.json' @ extension}
<!-- outputs: sourcedcode/home -->


// documentation
https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md


Common Questions for Writing Test Code for WCMUsePojo Classes
How do I pass a parameter to the WCMUsePojo class in AEM? Call the fully qualified Java class name in sightly, and use the @ symbol to pass the parameter. 


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.