workflowSession.adaptTo returning null session object | Junit | | Community
Skip to main content
Level 3
May 7, 2021
Solved

workflowSession.adaptTo returning null session object | Junit |

  • May 7, 2021
  • 1 reply
  • 1363 views

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

@veenavikraman 

@arunpatidar 

@Jörg_Hoh

 

 

 

 

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 Ritesh_Mittal
 

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;

 

1 reply

Ritesh_Mittal
Community Advisor and Adobe Champion
Ritesh_MittalCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
May 8, 2021
 

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;

 

NitinLAuthor
Level 3
May 10, 2021
Thank You @ritesh_mittal, it worked for me.. somehow I did not get any references about this.