How to mock a util class with static methods in Junit5 using wcm io?
Hi,
I am writing my first junit test for a workflow process and I am trying to write it using the AEM context (WCM io)
and my workflow process also has a util class method used in it. Here I am facing two problems one with
providing the context (which contains the resource resolver factory and the other objects) to the workflow process execute method in the test method and
the mocking of utils class in Test class.
Note: We can mock the util class if we use powermockito, but powermockito works only with Junit4, but I have to write my test case in Junit5.
below is an excerpt of my code:
@ExtendWith(AemContextExtension.class)
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class WorkflowProcessTest {
AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
@Mock
WorkItem workItem;
@Mock
WorkflowData workflowData;
@Mock
WorkflowSession workflowSession;
@Mock
MetaDataMap metaDataMap;
@Mock
private QueryBuilder queryBuilder;
@Mock
private Query qbQuery;
@Mock
ResourceResolverFactory resourceResolverFactory;
@Mock
private SearchResult qbResult;
@Mock
Session adminSession;
@Mock
ResourceResolver resolver;
WorkflowProcess workflowProcess;
@BeforeEach
void setUp() throws LoginException {
resourceResolverFactory = context.registerService(ResourceResolverFactory.class, new MockResourceResolverFactory());
context.registerAdapter(ResourceResolver.class, Session.class, mock(Session.class));
mockPayload();
resolver = context.resourceResolver();
when(resourceResolverFactory.getServiceResourceResolver(any())).thenReturn(resolver);
}
@2785667
void testExecute() throws RepositoryException,WorkflowException {
//Some more code here
workflowProcess = new WorkflowProcess();
workflowProcess.execute(workItem, workflowSession, metaDataMap);
//workflowProcess = context.registerInjectActivateService(new workflowProcess());
verifyMetadata(metadataNode);
}
private void verifyMetadata(Node metadataNode) throws RepositoryException {
//assert statement here
}
void mockPayload() {
//payload mock here
}
}
Can someone help on those two?
Your help is much appreciated.
Thanks in advance,
Adilakshmi


