Temporal Testing
The Temporal java SDK’s testing
package provides support for unit and functional testing of both Workflow and Activity code.
Test Workflow environment
The TestWorkflowEnvironment
class provides a runtime environment used to test a Workflow.
When running your Workflow code within the test environment, some aspects of its execution will work differently to better support testing. For example, Timers will “skip time” by firing without their normal delay, enabling you to quickly test long-running Workflows.
Test Workflow extension
The TestWorkflowExtention
class manages the Temporal test environment and workflow worker lifecycle. You’ll use this to register your Workflow Type and inject instances of TestWorkflowEnvironment
, WorkflowClient
, WorkflowOptions
, Worker
into test methods. This extension is registered with the testing framework using the @RegisterExtension
annotation from the JUnit package.
Example:
package mathworkflow
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import mathworkflow.SumOfSquaresWorkflowImpl;
import mathworkflow.SquaresActivities;
public class SumOfSquaresWorkflowTest {
@RegisterExtension
public static final TestWorkflowExtension testWorkflowExtension =
TestWorkflowExtension.newBuilder()
.setWorkflowTypes(SumOfSquaresWorkflowImpl.class)
.setDoNotStart(true)
.build();
@Test
public void testSumOfSquaresPositive(TestWorkflowEnvironment testEnv, Worker worker, SumOfSquaresWorkflow workflow) {
worker.registerActivitiesImplementations(new SquareActivitiesImpl());
testEnv.start();
int sumOfSquares = workflow.calculateSumOfSquares(5, 6);
assertEquals(61, sumOfSquares);
}
@Test
public void testSumOfSquaresNegative(TestWorkflowEnvironment testEnv, Worker worker, SumOfSquaresWorkflow workflow) {
worker.registerActivitiesImplementations(new SquareActivitiesImpl());
testEnv.start();
int sumOfSquares = workflow.calculateSumOfSquares(10, -9);
assertEquals(181, sumOfSquares);
}
}
Test Activity environment
The TestActivityEnvironment
type is similar to TestWorkflowEnvironment
, but used for testing Activities.