# Activity Test Environment

TestActivityEnvironment (opens new window) is the helper class for unit testing activity implementations. Supports calls to Activity methods from the tested activities. An example test:

   public interface TestActivity {
     String activity1(String input);

   private static class ActivityImpl implements TestActivity {
     public String activity1(String input) {
       return Activity.getTask().getActivityType().getName() + "-" + input;

   public void testSuccess() {
     testEnvironment.registerActivitiesImplementations(new ActivityImpl());
     TestActivity activity = testEnvironment.newActivityStub(TestActivity.class);
     String result = activity.activity1("input1");
     assertEquals("TestActivity::activity1-input1", result);

# Workflow Test Environment

TestWorkflowEnvironment provides workflow unit testing capabilities.

Testing the workflow code is hard as it might be potentially very long running. The included in-memory implementation of the Cadence service supports an automatic time skipping. Anytime a workflow under the test as well as the unit test code are waiting on a timer (or sleep) the internal service time is automatically advanced to the nearest time that unblocks one of the waiting threads. This way a workflow that runs in production for months is unit tested in milliseconds. Here is an example of a test that executes in a few milliseconds instead of over two hours that are needed for the workflow to complete:

public class SignaledWorkflowImpl implements SignaledWorkflow {
  private String signalInput;

  public String workflow1(String input) {
    Workflow.await(() -> signalInput != null);
    return signalInput + "-" + input;

  public void processSignal(String input) {
    signalInput = input;

public void testSignal() throws ExecutionException, InterruptedException {
 TestWorkflowEnvironment testEnvironment = TestWorkflowEnvironment.newInstance();

 // Creates a worker that polls tasks from the service owned by the testEnvironment.
 Worker worker = testEnvironment.newWorker(TASK_LIST);

 // Creates a WorkflowClient that interacts with the server owned by the testEnvironment.
 WorkflowClient client = testEnvironment.newWorkflowClient();
 SignaledWorkflow workflow = client.newWorkflowStub(SignaledWorkflow.class);

 // Starts a workflow execution
 CompletableFuture result = WorkflowClient.execute(workflow::workflow1, "input1");

 // The sleep forwards the service clock for 65 minutes without blocking.
 // This ensures that the signal is sent after the one hour sleep in the workflow code.

 // Blocks until workflow is complete. Workflow sleep forwards clock for one hour and
 // this call returns almost immediately.
 assertEquals("signalInput-input1", result.get());

 // Closes workers and releases in-memory service.