Testing pattern for Temporal Workflow - Java

Hi All,

I use Junit 4 to write unit tests in Java, but am looking for something similar for temporal. I went through the sample java tests provided by Temporal, but I could not find some things i was looking for like:

  • How do I simulate Activity failure in a Wokflow Unit test (Throwing some error from Activity mock)
  • How can I Simulate workflow Retry in Unit Test
  • How can I Simulate activity Retry if an Activity fails in a workflow
  • Is there a scope for simulating workflow failure?
  • How do I simulate Activity failure in a Wokflow Unit test (Throwing some error from Activity mock)

Here is the mockito-exceptions document.

  • How can I Simulate workflow Retry in Unit Test

Specify retry options when calling start workflow and then fail the workflow. Note that by default workflows don’t fail, but get stuck on an unknown exception. You can make workflow fail on certain exception types by specifying these types through ImplementationOptions. In this example, it specifies Throwable as the exception type. So workflow will fail immediately on any exception.

    worker.registerWorkflowImplementationTypes(
        WorkflowImplementationOptions.newBuilder()
            .setFailWorkflowExceptionTypes(NonRetryableException.class)
            .build(),
        AccountTransferWorkflowImpl.class);

Note that you usually do want your workflows to get stuck on unknown exceptions instead of failing. It allows deploying a fix to the problem without failing the workflows.

Workflow retries are usually a bad idea. It is better to write workflow in a way that it never fails.

  • How can I Simulate activity Retry if an Activity fails in a workflow

It is already retried by the unit testing framework according to its retry policy.

  • Is there a scope for simulating workflow failure?

I’m not sure I understand the question. You can always write a fake workflow that fails and let the client code handle the failure appropriately.

Hey Maxim,

Thanks for the quick reply:

Here is 1 the mockito-exceptions document.

I had tried this:

WorkflowOptions options =
    WorkflowOptions.newBuilder()
        .setTaskQueue(TASK_QUEUE)
        .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(2).build())
        .build();
AccountTransferWorkflow workflow =
    workflowClient.newWorkflowStub(AccountTransferWorkflow.class, options);
long starty = testEnv.currentTimeMillis();

doThrow(new ActivityCanceledException())
    .when(activities)
    .withdraw(eq("account1"), eq("reference1"), eq(123));

How do I specify a maximum number of activity Retries for a mocked Activity? Currently, it is going on an endless loop;

And Also Could you please tell me what could be a good Scope of testing a Workflow - i.e what are the scenarios to test in a workflow? Since activity testing will be more around testing the service.

How do I specify a maximum number of activity Retries for a mocked Activity? Currently, it is going on an endless loop;

The unit test executes according to the activity retry policy. By default, the retry policy is going to retry up to workflow run timeout which defaults to infinity. Practically it means that you don’t need to test this activity failure as in production scenarios it is never going to fail to the workflow code due to infinite retries.

And Also Could you please tell me what could be a good Scope of testing a Workflow - i.e what are the scenarios to test in a workflow? Since activity testing will be more around testing the service.

You want to test all possible code paths in the workflow if possible. Find out which activities have retry policies that allow them to fail before workflow is completed and mock their failures. And if your code paths depend on data, then simulate data to go over those code paths.

Thanks for this! Much appreciated