Testing a signal method within a workflow

Hello!

I have a signal method which I am calling from a child workflow. I’m trying to test and verify that this method is actually getting called. Is there a way that I can mock the parent workflow that contains the signal method implementation?

Here is an example:

public class ParentWorkflowImpl implements ParentWorkflow {

	private final ChildWorkflow child = Workflow.newWorkflowStub(
		ChildWorkflow.class,
		CHILD_WORKFLOW_OPTIONS
	);	

	@Override
	public void signalMethod() {
    	System.out.println("Signal method called");
  	}
}

public class ChildWorkflowImpl implements ChildWorkflow {
	private final ChildActivities childActivities =
		Workflow.newActivityStub(
		    ChildActivities.class,
		    CHILD_ACTIVITY_OPTIONS
		);

	@Override
	public ChildResponse run(final ChildRequest request) {
		...
		ParentWorkflow parentWorkflow = Workflow.newExternalWorkflowStub(
			ParentWorkflow.class,
			String.valueOf(request.getParentWorkflowId())
		);
        // This is what I'm trying to verify is being called
        parentWorkflow.signalMethod();
	}
}

In my TestChildWorkflow test, is it possible to mock the parent workflow in a way that verifies my signal method is being called?

2 Likes

Your mock workflow can be something like:

public class ParentWorkflowImpl implements ParentWorkflow {

    private final ChildWorkflow child = Workflow.newWorkflowStub(
            ChildWorkflow.class,
            CHILD_WORKFLOW_OPTIONS
    );

    private CompletablePromise<Void> signaled = Workflow.newPromise();

    @Override
    public void run() {
       Async.function(child::run, request);
        try {
            signaled.get(10, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            throw  Workflow.wrap(e);
        }
    }

    @Override
    public void signalMethod() {
        signaled.complete(null);
    }
}
1 Like