JUnit 4: AEM WCMUsePojo Unit Test Example (Passing Paramaters)

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 path 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:

1
2
3
4
5
6
<!-- somecomponent -->
<div class="somecomponent">
    <a href="${lnk.link}" target="_blank" rel="noopener noreferrer">
        Click Here
    </div>
</div>

WCMUsePojo Example Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 utilise 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
${'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. Take a look a the example above example

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top