how to get content fragment model name from contentfragment object | Community
Skip to main content
Level 2
March 20, 2023
Solved

how to get content fragment model name from contentfragment object

  • March 20, 2023
  • 2 replies
  • 1434 views

how to get content fragment model name from contentfragment object which support junit test case.
Note: contentFragment.getTemplate().getTitle() is not supportive in junit mocking.

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 ShaileshBassi

@sudarshan1992 

@Test void getContentFragmentTest() throws ContentFragmentException { final ContentFragment contentFragment = mock(ContentFragment.class); getValue(contentFragment, TITLE, "MyTitle"); assertEquals("MyTitle", contentFragmentModel.getTitle()); } private static void getValue(ContentFragment contentFragment, String key, String value) { ContentElement contentElement = mock(ContentElement.class); FragmentData fragmentData = mock(FragmentData.class); when(contentFragment.getElement(key)).thenReturn(contentElement); when(contentElement.getValue()).thenReturn(fragmentData); when(fragmentData.getValue()).thenReturn(value); }

You might need to mock everything and try.
Was able to get the junit coverage with the above code. 
Hope this helps!

Thanks

2 replies

Jagadeesh_Prakash
Community Advisor
Community Advisor
March 22, 2023

@sudarshan1992 

you can get the content fragment model name from a content fragment object by calling the getTemplatePath()

 

Can you try below code 

 

import com.adobe.cq.dam.cfm.ContentFragment;
import org.junit.Test;

public class ContentFragmentTest {

@Test
public void testGetContentFragmentModelName() {
ContentFragment contentFragment = // Create or obtain a ContentFragment object
String modelName = contentFragment.getTemplatePath();
System.out.println("Content Fragment Model Name: " + modelName);
}

}

Level 2
March 23, 2023

contentFragment.getTemplatePath(); --> this method itself doesnot exist with 

contentFragment.

Jagadeesh_Prakash
Community Advisor
Community Advisor
March 31, 2023
ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
April 5, 2023

@sudarshan1992 

@Test void getContentFragmentTest() throws ContentFragmentException { final ContentFragment contentFragment = mock(ContentFragment.class); getValue(contentFragment, TITLE, "MyTitle"); assertEquals("MyTitle", contentFragmentModel.getTitle()); } private static void getValue(ContentFragment contentFragment, String key, String value) { ContentElement contentElement = mock(ContentElement.class); FragmentData fragmentData = mock(FragmentData.class); when(contentFragment.getElement(key)).thenReturn(contentElement); when(contentElement.getValue()).thenReturn(fragmentData); when(fragmentData.getValue()).thenReturn(value); }

You might need to mock everything and try.
Was able to get the junit coverage with the above code. 
Hope this helps!

Thanks