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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

workflowSession.adaptTo returning null session object | Junit |

Avatar

Level 3

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 

@arunpatidar 

@Jörg_Hoh

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
 

Hi @NitinL ,

 

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

Avatar

Correct answer by
Community Advisor
 

Hi @NitinL ,

 

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;

 

Avatar

Level 3
Thank You @Ritesh_Mittal, it worked for me.. somehow I did not get any references about this.