Class TestWorkflowRule

java.lang.Object
io.temporal.testing.TestWorkflowRule
All Implemented Interfaces:
org.junit.rules.TestRule

public class TestWorkflowRule extends Object implements org.junit.rules.TestRule
JUnit4

Test rule that sets up test environment, simplifying workflow worker creation and shutdown. Can be used with both in-memory and standalone temporal service. (see TestWorkflowRule.Builder.setUseExternalService(boolean) and TestWorkflowRule.Builder.setTarget(String)})

Example of usage:


   public class MyTest {

  @Rule
   public TestWorkflowRule workflowRule =
       TestWorkflowRule.newBuilder()
           .setWorkflowTypes(TestWorkflowImpl.class)
           .setActivityImplementations(new TestActivities())
           .build();

  @Test
   public void testMyWorkflow() {
       TestWorkflow workflow = workflowRule.getWorkflowClient().newWorkflowStub(
                 TestWorkflow.class, WorkflowOptions.newBuilder().setTaskQueue(workflowRule.getTaskQueue()).build());
       ...
   }
 
  • Method Details

    • createTestEnvOptions

      protected TestEnvironmentOptions createTestEnvOptions(long initialTimeMillis)
    • newBuilder

      public static TestWorkflowRule.Builder newBuilder()
    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
      Specified by:
      apply in interface org.junit.rules.TestRule
    • shutdown

      protected void shutdown()
    • isUseExternalService

      public boolean isUseExternalService()
      Returns:
      true if the rule is using external temporal service.
    • getTestEnvironment

      public TestWorkflowEnvironment getTestEnvironment()
    • getTaskQueue

      public String getTaskQueue()
      Returns:
      name of the task queue that test worker is polling.
    • getWorkerOptions

      public io.temporal.worker.WorkerOptions getWorkerOptions()
      Returns:
      The options used for the worker.
    • getNexusEndpoint

      public io.temporal.api.nexus.v1.Endpoint getNexusEndpoint()
      Returns:
      endpoint of the nexus service created for the test.
    • getWorkflowClient

      public io.temporal.client.WorkflowClient getWorkflowClient()
      Returns:
      client to the Temporal service used to start and query workflows.
    • getActivityClient

      public io.temporal.client.ActivityClient getActivityClient()
      Returns:
      client to the Temporal service used to start standalone activities.
    • getWorkflowServiceStubs

      public io.temporal.serviceclient.WorkflowServiceStubs getWorkflowServiceStubs()
      Returns:
      stubs connected to the test server (in-memory or external)
    • blockingStub

      public io.temporal.api.workflowservice.v1.WorkflowServiceGrpc.WorkflowServiceBlockingStub blockingStub()
      Returns:
      blockingStub
    • getInterceptor

      public <T extends io.temporal.common.interceptors.WorkerInterceptor> T getInterceptor(Class<T> type)
      Returns:
      tracer.
    • getHistory

      @Deprecated public io.temporal.api.history.v1.History getHistory(@Nonnull io.temporal.api.common.v1.WorkflowExecution execution)
      Deprecated.
      use WorkflowClient.fetchHistory(String, String). To obtain a WorkflowClient use getWorkflowClient()
      Returns:
      workflow execution history
    • getWorkflowExecutionHistory

      @Deprecated public io.temporal.api.history.v1.History getWorkflowExecutionHistory(io.temporal.api.common.v1.WorkflowExecution execution)
      Deprecated.
      use WorkflowClient.fetchHistory(String, String). To obtain a WorkflowClient use getWorkflowClient()
      Returns:
      name of the task queue that test worker is polling.
    • getWorker

      public io.temporal.worker.Worker getWorker()
      This worker listens to the default task queue which is obtainable via the getTaskQueue() method.
      Returns:
      the default worker created for each test method.
    • getWorkerFactoryOptions

      public io.temporal.worker.WorkerFactoryOptions getWorkerFactoryOptions()
    • newWorkflowStub

      public <T> T newWorkflowStub(Class<T> workflow)
    • newUntypedWorkflowStub

      public io.temporal.client.WorkflowStub newUntypedWorkflowStub(String workflow)
    • getUniquePostfix

      public String getUniquePostfix()
      Returns:
      A unique string containing the test name appended to the task queue used by the test worker. Can be used for other test-specific naming.