Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

Page.getTags() junits


Level 6

HI Team,

I was able to get all properties of page when stubbed through json but not cq:tags,

when I do page.getPath() it's getting the path but it's returning null for page.getTags() however I was able to see cq:tags property is not null it has tags when i debug it 



8 Replies


Community Advisor


How are you mocking the page? Can you post some code to see what is going on?

Esteban Bustamante


Level 6



aemContext.request().setAttribute("productModelPage", "/content/Page");

here is my model code:

productModelPage is a variable 

PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page productPage = pageManager.getPage(productModelPage);
String path = productPage.getPath();
Tag[] prodcutPageTags = productPage.getTags();


Community Advisor

You should create the page and load its properties as part of the aemContext so below line actually can retrieve the page's properties:


Try something like this https://wcm.io/testing/aem-mock/usage-content-loader-builder.html:



Hope this helps


Esteban Bustamante


Level 6

ok, but productmodelPage is a string variable in my model so setting it as attribute.


Community Advisor

Right, the attribute will tell which resource to look in a specific path, but you need to load such resource in the path, so the AEMcontext can get it

Esteban Bustamante


Level 10

HI @Keerthi0555 ,

To write JUnit tests for `Page.getTags()`, you can use the AEM Mocks library provided by Adobe. Here's an example of how you can write JUnit tests for `Page.getTags()`:

1. Add the AEM Mocks dependency to your project's build.gradle or pom.xml file:

For Gradle:
testImplementation 'com.adobe.aem:uber-jar:6.5.0:tests@jar'

For Maven:

2. Write a JUnit test method to test `Page.getTags()`:

import com.adobe.cq.wcm.core.components.models.Page;
import io.wcm.testing.mock.aem.junit.AemContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PageTest {

public final AemContext context = new AemContext();

private Page page;

public void setUp() {
// Create a mock Page object
page = mock(Page.class);

// Set up the AemContext with the mock Page

public void testGetTags() {
// Set up the mock behavior for getTags()
String[] tags = {"tag1", "tag2"};

// Call the method being tested
String[] result = page.getTags();

// Assert the expected result
assertEquals(tags, result);

In this example, we use the AemContext provided by AEM Mocks to set up a mock Page object. We then use Mockito to mock the behavior of `Page.getTags()` and return a predefined array of tags. Finally, we call `page.getTags()` and assert that the result matches the expected tags.

Make sure to import the necessary classes and adjust the version of the AEM Mocks library according to your AEM version.

Note: The AEM Mocks library provides a comprehensive set of mock objects and utilities for testing AEM components and services. You can explore more features and examples in the AEM Mocks documentation.


Community Advisor

When using the wcm.io mocks library in your JUnit5 tests for AEM, ensuring that cq:tags are correctly retrieved requires proper setup of the mock objects and their properties. Here's how you can make sure cq:tags are correctly mocked and accessible in your tests.

Steps to Mock cq:tags Property Correctly:

  1. Include Necessary Dependencies: Ensure you have the correct dependencies for wcm.io mocks and JUnit5 in your project. Add these dependencies to your pom.xml or build.gradle:

  2. Set Up the Mock AEM Context: Use AemContext to set up your test context, and ensure that you properly set the cq:tags property.

Here's an example of how to set up your test to mock the cq:tags property using wcm.io mocks and JUnit5:

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import io.wcm.testing.mock.aem.junit5.AemContext;
import org.apache.sling.api.resource.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

public class PageTagsTest {

    private final AemContext context = new AemContext();

    public void setUp() {
        // Create a test page with cq:tags
        context.load().json("/sample-content.json", "/content/sample");

        // Optionally, mock the TagManager if needed
        TagManager tagManager = mock(TagManager.class);
        context.registerService(TagManager.class, tagManager);

        Tag tag = mock(Tag.class);

    public void testPageTags() {
        // Get the page
        PageManager pageManager = context.pageManager();
        Page page = pageManager.getPage("/content/sample");

        assertNotNull(page, "Page should not be null");
        assertEquals("/content/sample", page.getPath());

        // Retrieve the tags
        Tag[] tags = page.getTags();
        assertNotNull(tags, "Tags should not be null");
        assertEquals(1, tags.length);
        assertEquals("sample:tag", tags[0].getTagID());


JSON Content Setup:

Ensure your sample-content.json includes the cq:tags property. Here’s an example JSON snippet:

  "jcr:primaryType": "cq:Page",
  "jcr:content": {
    "jcr:primaryType": "cq:PageContent",
    "cq:tags": ["sample:tag"]


  1. Dependencies: The dependencies for io.wcm.testing.aem-mock.junit5 are included to enable AEM mocking for JUnit5.

  2. Setup: The setUp() method loads a JSON file that represents the content structure, including the cq:tags property.

  3. Mocking TagManager: The TagManager is mocked to resolve the tag ID correctly. This ensures that when page.getTags() is called, it returns the appropriate tag.

  4. Test Case: The test case retrieves the page and asserts that the cq:tags property is correctly mocked and accessible.

By following these steps and ensuring the proper setup of your mock context and JSON content, you should be able to retrieve the cq:tags property in your unit tests using the wcm.io mocks library in JUnit5.




@Keerthi0555 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni