Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

Unable to read the elements in MockContentFragment object.

Avatar

Level 1

Created the below content fragment json.

{
"jcr:primaryType": "cq:ContentFragment",
"jcr:content": {
"jcr:primaryType": "nt:unstructured",
"title": "My Test Content Fragment",
"description": "This is a test content fragment for JUnit.",
"elements": {
"jcr:primaryType": "nt:unstructured",
"headline": {
"jcr:primaryType": "nt:unstructured",
"value": "Test Headline"
},
"body": {
"jcr:primaryType": "nt:unstructured",
"value": "<p>This is the body text of the content fragment.</p>"
}
}
}
}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @SureshVadapalli,

Could you pass the JUnit here to better understand you resolve your query?
Try this:

{
  "jcr:primaryType": "dam:Asset",
  "jcr:content": {
    "jcr:primaryType": "dam:AssetContent",
    "data": {
      "jcr:primaryType": "nt:resource"
    },
    "metadata": {
      "jcr:primaryType": "nt:unstructured",
      "dc:title": "My Test Content Fragment"
    },
    "contentFragment": true,
    "elements": {
      "jcr:primaryType": "nt:unstructured",
      "headline": {
        "jcr:primaryType": "nt:unstructured",
        "value": "Test Headline"
      },
      "body": {
        "jcr:primaryType": "nt:unstructured",
        "value": "<p>This is the body text of the content fragment.</p>"
      }
    }
  }
}
@ExtendWith(AemContextExtension.class)
class ContentFragmentTest {

    private final AemContext context = new AemContext();

    @BeforeEach
    void setUp() {
        context.load().json("/test-content-fragment.json", "/content/dam/test");
    }

    @Test
    void testReadContentFragmentElements() {
        Resource cfResource = context.resourceResolver().getResource("/content/dam/test");
        ContentFragment contentFragment = cfResource.adaptTo(ContentFragment.class);

        assertNotNull(contentFragment);
        assertEquals("Test Headline", contentFragment.getElement("headline").getContent());
        assertEquals("<p>This is the body text of the content fragment.</p>",
                     contentFragment.getElement("body").getContent());
    }
}

Santosh Sai

AEM BlogsLinkedIn


View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @SureshVadapalli,

Could you pass the JUnit here to better understand you resolve your query?
Try this:

{
  "jcr:primaryType": "dam:Asset",
  "jcr:content": {
    "jcr:primaryType": "dam:AssetContent",
    "data": {
      "jcr:primaryType": "nt:resource"
    },
    "metadata": {
      "jcr:primaryType": "nt:unstructured",
      "dc:title": "My Test Content Fragment"
    },
    "contentFragment": true,
    "elements": {
      "jcr:primaryType": "nt:unstructured",
      "headline": {
        "jcr:primaryType": "nt:unstructured",
        "value": "Test Headline"
      },
      "body": {
        "jcr:primaryType": "nt:unstructured",
        "value": "<p>This is the body text of the content fragment.</p>"
      }
    }
  }
}
@ExtendWith(AemContextExtension.class)
class ContentFragmentTest {

    private final AemContext context = new AemContext();

    @BeforeEach
    void setUp() {
        context.load().json("/test-content-fragment.json", "/content/dam/test");
    }

    @Test
    void testReadContentFragmentElements() {
        Resource cfResource = context.resourceResolver().getResource("/content/dam/test");
        ContentFragment contentFragment = cfResource.adaptTo(ContentFragment.class);

        assertNotNull(contentFragment);
        assertEquals("Test Headline", contentFragment.getElement("headline").getContent());
        assertEquals("<p>This is the body text of the content fragment.</p>",
                     contentFragment.getElement("body").getContent());
    }
}

Santosh Sai

AEM BlogsLinkedIn