Expand my Community achievements bar.

SOLVED

AEM Junits @Reference Mock

Avatar

Level 2

Hi All,
We have a requirement to write the JUnit's, while writing I am encountering below @reference, 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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Vishal_Jain03  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.

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

@Vishal_Jain03  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.

Avatar

Administrator

@Vishal_Jain03Did 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