Expand my Community achievements bar.

SOLVED

JUNIT issue for creating content fragment programmatically

Avatar

Community Advisor

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

1 Accepted Solution

Avatar

Correct answer by
Level 9

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.

View solution in original post

4 Replies

Avatar

Community Advisor

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.

Avatar

Correct answer by
Level 9

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.

Avatar

Community Advisor

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

 

context.currentResource("/content/...").adaptTo(FragmentTemplate.class);
context.request().adaptTo(FragmentTemplate.class);

Avatar

Level 7

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