AEM Junits @Reference Mock | Community
Skip to main content
Level 3
June 10, 2024
Solved

AEM Junits @Reference Mock

  • June 10, 2024
  • 3 replies
  • 816 views

Hi All,
We have a requirement to write the JUnit's, while writing I am encountering below @3214626, how to mock this in AEMAACS for workflow?

@Reference
QueryBuilder queryBuilder; //null
@Reference
private DamPropertiesConfiguration damconfig; //null
@Reference
ResourceResolverFactory resourceResolverFactory; /null

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
{
try
{
ExampleWorkflowHelper exampleWorkflowHelper = new ExampleWorkflowHelper();
ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);
log.info("workflow started");
exampleWorkflowHelper.generateDamAssets(resolver, queryBuilder, damconfig);
}
}

above is the aem workflow code for that we need to write junits 
and How to pass @Reference variable as a parameter in the below method
exampleWorkflowHelper.generateDamAssets(resolver, queryBuilder, damconfig);

Thanks & Regards

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 Harwinder-singh

@vishal33  use @Mock instead of @reference 

Make sure that you setup any objects / object attributes in the Setup method (this is the one with @BeforeEach annotation)

For instance, if you need the ResourceResolver from the ResoruceResolverFactory mock object, the inside the setup method, you should have something like this : 

 

lenient().when(resourceResolverFactory.getServiceResourceResolver(Mockito.anyMap())).thenReturn(resourceResolver)

 

resourceResolver is a mock ResourceResolver object here that can in turn be mapped to a path using something similar as above.

 

Hope this helps.

3 replies

Harwinder-singh
Community Advisor
Harwinder-singhCommunity AdvisorAccepted solution
Community Advisor
June 10, 2024

@vishal33  use @Mock instead of @reference 

Make sure that you setup any objects / object attributes in the Setup method (this is the one with @BeforeEach annotation)

For instance, if you need the ResourceResolver from the ResoruceResolverFactory mock object, the inside the setup method, you should have something like this : 

 

lenient().when(resourceResolverFactory.getServiceResourceResolver(Mockito.anyMap())).thenReturn(resourceResolver)

 

resourceResolver is a mock ResourceResolver object here that can in turn be mapped to a path using something similar as above.

 

Hope this helps.

sravs
Community Advisor
Community Advisor
June 11, 2024
kautuk_sahni
Community Manager
Community Manager
June 13, 2024

@vishal33Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni