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

workflowSession.adaptTo returning null session object | Junit |

Avatar

Avatar
Give Back
Level 1
mynitumail
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

0 solutions
Top badges earned
Give Back
Ignite 1
View profile

Avatar
Give Back
Level 1
mynitumail
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

0 solutions
Top badges earned
Give Back
Ignite 1
View profile
mynitumail
Level 1

07-05-2021

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Boost 5
Level 5
Ritesh_M
Level 5

Likes

85 likes

Total Posts

67 posts

Correct Reply

23 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile

Avatar
Boost 5
Level 5
Ritesh_M
Level 5

Likes

85 likes

Total Posts

67 posts

Correct Reply

23 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile
Ritesh_M
Level 5

08-05-2021

 

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;

 

Answers (0)