Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

workflowSession.adaptTo returning null session object | Junit |

mynitumail
Level 2
Level 2

Trying to get MOCK session object in workflow process class to satisfy below statement, but getting null value for jcrSession.

Session jcrSession = workflowSession.adaptTo(Session.class);

Code Snippet from Junit class

==================================

public final AemContext context = new AemContext();

@BeforeEach
public void setup() throws Exception {

workflowSessionMock = Mockito.mock(WorkflowSession.class);
workflowModelMock = Mockito.mock(WorkflowModel.class);
workItem = Mockito.mock(WorkItem.class);
wfData = Mockito.mock(WorkflowData.class);
wfMetaDataMap = Mockito.mock(MetaDataMap.class);
wfWorkflow = Mockito.mock(Workflow.class);
mockSession = Mockito.mock(Session.class);
}

@Test
void testExecute() throws WorkflowException {


MetaDataMap metaData = new SimpleMetaDataMap();
StringBuilder args = new StringBuilder();
args.append("effectiveDate=meta:absoluteTime");
Mockito.when(workItem.getWorkflow()).thenReturn(wfWorkflow);
Mockito.when(wfWorkflow.getMetaDataMap()).thenReturn(wfMetaDataMap);
context.registerAdapter(WorkflowSession.class, Session.class, mockSession);
Mockito.when(wfData.getPayloadType()).thenReturn("JCR_PATH");
Mockito.when(wfData.getPayload()).thenReturn("/content/dam/page");
Mockito.when(workItem.getWorkflowData()).thenReturn(wfData);
String sbString = args.toString();
String[] ary = sbString.split("=");

metaData.put("PROCESS_ARGS", ary);
process.execute(workItem, workflowSessionMock, metaData);

}

Getting all the required parameters(workItem, workflowSession, metaDataMap) and assigned values in workflow process class, but workflowSession.adaptTo is returning null Session object. 

 

Any thoughts or suggestion please.

AEM SDK Version - aem-sdk-2020.12.4676.20201216T130744Z-201201
Archtype - 23
==

Thanks

@Veena_Vikram 

@Arun_Patidar 

@Jörg_Hoh

 

 

 

 

AemContext junit WorkflowSession
1 Accepted Solution
Ritesh_M
Correct answer by
Community Advisor
Community Advisor
 

Hi @mynitumail ,

 

you need to add below statement also in your unit test execute method before calling process.execute

 

when(workflowSessionMock.adaptTo(Session.class)).thenReturn(session);

 

and add a Session mock object in the unit class, like below

 

@Mock
private Session session;

 

View solution in original post

2 Replies
Ritesh_M
Correct answer by
Community Advisor
Community Advisor
 

Hi @mynitumail ,

 

you need to add below statement also in your unit test execute method before calling process.execute

 

when(workflowSessionMock.adaptTo(Session.class)).thenReturn(session);

 

and add a Session mock object in the unit class, like below

 

@Mock
private Session session;

 

View solution in original post

mynitumail
Level 2
Level 2
Thank You @Ritesh_M, it worked for me.. somehow I did not get any references about this.