Unable to read the elements in MockContentFragment object. | Community
Skip to main content
August 7, 2025
Solved

Unable to read the elements in MockContentFragment object.

  • August 7, 2025
  • 1 reply
  • 255 views

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>"
}
}
}
}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SantoshSai

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()); } }

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
August 7, 2025

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