JUNIT issue for creating content fragment programmatically | Community
Skip to main content
TarunKumar
Community Advisor
Community Advisor
January 22, 2025
Solved

JUNIT issue for creating content fragment programmatically

  • January 22, 2025
  • 4 replies
  • 702 views

Hi All,

I am trying to write a JUNIT class for my core class which is basically creating CF programmatically.
Core class:

public void createContentFragment() { Resource destCFFolderResource = resolver.getResource(destCfFolderPath); Resource cfModelResource = resolver.getResource("/conf/<cf-model-path>"); if (null != cfModelResource) { FragmentTemplate fragmentTemplate = cfModelResource.adaptTo(FragmentTemplate.class); if (null != fragmentTemplate) { newContentFragment = fragmentTemplate.createFragment(destCFFolderResource,"contentFragmentName","Description"); } } }

 

I am writing JUNIT5 using aemcontext by loading json as resource.
Everything is fine but below line is giving null for fragmentTemplate though I'm getting cfModelResource value.

 

FragmentTemplate fragmentTemplate = cfModelResource.adaptTo(FragmentTemplate.class);


Any hint would be really appreciated!

 

 

-Tarun

Best answer by Tethich

Hi @tarunkumar


What we did on our project was to create our own simple mock implementation of the FragmentTemplate (cause' this is an interface).

Then, in the JUnit test class, in the @BeforeAll step, we registered a new adapter with this mock class as the handler.

@BeforeAll static void init() { context.registerAdapter(Resource.class, FragmentTemplate.class, (Function<Resource, FragmentTemplate>) FragmentTemplateMock::new); }


Here the context object is the wcm.io AemContext, which I understand you already have

private final AemContext context = new AemContext();


If you will try this, please let me know if it works. I am also interested to get extra confirmations. Same if you find another way.

4 replies

iamnjain
Community Advisor
Community Advisor
January 22, 2025

Hi @tarunkumar 

Can you share snippet from Test Class as well to understand this better?
I worked on similar CF Utility and wrote the test case.

Tethich
Community Advisor
TethichCommunity AdvisorAccepted solution
Community Advisor
January 22, 2025

Hi @tarunkumar


What we did on our project was to create our own simple mock implementation of the FragmentTemplate (cause' this is an interface).

Then, in the JUnit test class, in the @BeforeAll step, we registered a new adapter with this mock class as the handler.

@BeforeAll static void init() { context.registerAdapter(Resource.class, FragmentTemplate.class, (Function<Resource, FragmentTemplate>) FragmentTemplateMock::new); }


Here the context object is the wcm.io AemContext, which I understand you already have

private final AemContext context = new AemContext();


If you will try this, please let me know if it works. I am also interested to get extra confirmations. Same if you find another way.

Anudeep_Garnepudi
Community Advisor
Community Advisor
January 22, 2025

@tarunkumar Check if any of the below two methods works.

 

context.currentResource("/content/...").adaptTo(FragmentTemplate.class);
context.request().adaptTo(FragmentTemplate.class);
anupampat
Community Advisor
Community Advisor
January 22, 2025

Hi @tarunkumar ,

 

Below code worked for me, hope it helps

 

//Mock FragmentTemplate @Mock FragmentTemplate fragmentTemplate; context.registerAdapter(Resource.class, FragmentTemplate.class, fragmentTemplate); ContentFragment contentFragment = mock(ContentFragment.class); lenient().when(fragmentTemplate.createFragment(any(Resource.class), any(), any())).thenReturn(contentFragment);

 

Regards,

Anupam Patra