AEM Mock page.getTags() return 0 Tags



Hi Fellow AEM Developers,

I'm writing unit test for the SlingModel for a project that uses AEM 6.1. Sling model check if the Page has a Tag or not in the Post construct method. During the test mocked a Page with json which contains the "cq:tags" property, however the mocked page returns 0 tags when page.getTags() called.

Here are the code snippets.



@Model(adaptables = SlingHttpServletRequest.class)

public class SomeModel {



    private ResourceResolver resourceResolver;


  public void postConstruct() {

  PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

  Page page = pageManager.getPage("/content/newssite/en/category");

  boolean hasTag = isPageHasTag(page);


  public static boolean isPageHasTag(Page page) {


      Tag[] tags = page.getTags();

       for(Tag tag : tags) {

            if(tag.getTagID().equals("news-site:news-types/sports")) {

            return true;



       return false;



Now i have to call this is in postConstruct() since the actual logic is lot more than this.




  "jcr:primaryType": "cq:Page",

  "jcr:content": {

  "jcr:primaryType": "nt:unstructured",

  "jcr:title": "News",

  "imagePath": "/content/dam/news-site/image.png",

  "hideInNav": "false",

  "cq:tags": ["news-site:news-types/sports"],

  "sling:resourceType": "newssite/components/structure/generic-page"



tried with

cq:tags": "[news-site:news-types/sports]" as well

Test Code



public class TopNavControllerTest {


  public AemContext context = new AemContext();


    public void setup() throws Exception {

  context.load().json("/test/category_page.json", "/content/newssite/en/category");

  Resource categoryPageResource = context.resourceResolver().getResource("/content/newssite/en/category");




    public void testPostConstruct() {

  //This should call postConstruct() and check Tag

  SomeModel someModel = context.request().adaptTo(SomeModel.class);












I'm not sure what's wrong.

Any help is much appreciated.

Thanks so much in Advance.





I ran into the same issue. in order to resolve Tags, they must exists under `/content/cq:tags/your/tag` or `/etc/tags` (legacy).

The Page#getTags implementation makes a call to TagManager#getTags which in turn tries to resolve the actual tag resource in the repo. Since you are testing in an AEM context, you have to load these tags in the appropriate location for the MockTagManager to resolve them.

What this means is that you need to load your tags into the AEM test context just like you've loaded your resources (via json).

Take a look at the aem-mock TagManager impl here: wcm-io-testing/MockTagManager.java at develop · wcm-io/wcm-io-testing · GitHub start with the `resolve` method and debug your way to figure out where you need to add those tags.