Hi,
I have an activity implementation as below.
ActivityImpl.java
@Component
public class ActivityImpl implements Activity {
@Autowired
WorkflowClient workflowClient;
@Autowired
SomeService service;
@Override
public void initializeActivity(Long id) {
ServiceDTO serviceDTO = null;
try {
serviceDTO = service.doOperation(id);
} catch (CustomWorkflowException e) {
log.info("Failed to initiate activity for {} : {}", id, e);
throw Activity.wrap(e);
}
SomeWorkflow workflow = workflowClient.newWorkflowStub(
SomeWorkflow.class, getWorkflowId());
workflow.signalActivityInitialized(serviceDTO.getId());
}
private String getWorkflowId() {
return Activity.getExecutionContext().getInfo().getWorkflowId();
}
}
Basically it has to call a service, do some operation and then signal that its done with its execution.
I am trying to Unit test this activity class. But am getting “workflow” as null and testcase fails with null pointer exception.
ActivityTest.java
public class ActivityTest {
@InjectMocks
private ActivityImpl activityImpl;
@Mock
private SomeService someService;
@Mock
private WorkflowClient client;
private TestWorkflowEnvironment testEnv;
private TestActivityEnvironment testActivityEnv;
private Worker worker;
@Rule
public TestWatcher watchman =
new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
if (testEnv != null) {
System.err.println(testEnv.getDiagnostics());
testEnv.close();
}
}
};
@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
UserContextManager.createContext(new User("1", null, null));
testActivityEnv = TestActivityEnvironment.newInstance();
testEnv = TestWorkflowEnvironment.newInstance();
worker = testEnv.newWorker(RUN_QUEUE);
worker.registerWorkflowImplementationTypes(SomeWorkflowImpl.class);
client = testEnv.getWorkflowClient();
}
@After
public void tearDown() {
testActivityEnv.close();
testEnv.close();
}
@Rule
public TestWorkflowRule testWorkflowRule =
TestWorkflowRule.newBuilder()
.setWorkflowTypes(SomeWorkflowImpl.class)
.setDoNotStart(true)
.build();
@Test
public void testInitializeActivityRun() throws CustomWorkflowException {
ServiceDTO serviceDTO = ServiceDTO.builder().id(1L).build();
when(someService.doOperation(1L)) .thenReturn(serviceDTO);
worker.registerActivitiesImplementations(activityImpl);
testEnv.start();
WorkflowOptions options =
WorkflowOptions.newBuilder().setTaskQueue(RUN_QUEUE)
.setContextPropagators(new UserContextPropagator())
.setWorkflowId("WFL-1").build();
SomeWorkflow workflow =
client.newWorkflowStub(SomeWorkflow.class, options);
WorkflowExecution response = WorkflowClient.start(workflow::run, 1L);
System.out.println("workflow exec:" + response);
testEnv.sleep(Duration.ofSeconds(2));
WorkflowStub.fromTyped(workflow).getResult(String.class);
verify(someService.atLeast(1)).doOperation(1L);
testEnv.shutdown();
}
}
Now when I run the test case, am getting null pointer exception
java.lang.NullPointerException: Cannot invoke “…SomeWorkflow.signalActivityInitialized(java.lang.Long)” because “workflow” is null.
However, in the getWorkflowId() method am able to get the workflow id from activity context. But when I do workflowClient.newWorkflowStub(SomeWorkflow.class, getWorkflowId()), it returns null.
Could you please let me know if something is wrong here, I want to test the activity class. I tried using TestActivityEnvironment but with no luck.