Class TestWorkflowRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class TestWorkflowRule
    extends java.lang.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 Detail

      • 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()
      • getTaskQueue

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

        public io.temporal.client.WorkflowClient getWorkflowClient()
        Returns:
        client to the Temporal service used to start and query workflows.
      • 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​(java.lang.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.
      • newWorkflowStub

        public <T> T newWorkflowStub​(java.lang.Class<T> workflow)
      • newUntypedWorkflowStub

        public io.temporal.client.WorkflowStub newUntypedWorkflowStub​(java.lang.String workflow)