Interface TestActivityEnvironment
- All Known Implementing Classes:
TestActivityEnvironmentInternal
public interface TestActivityEnvironment
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 {
@Override
public String activity1(String input) {
return Activity.getExecutionContext().getInfo().getActivityType() + "-" + input;
}
}
@Test
public void testSuccess() {
testEnvironment.registerActivitiesImplementations(new ActivityImpl());
TestActivity activity = testEnvironment.newActivityStub(TestActivity.class);
String result = activity.activity1("input1");
assertEquals("TestActivity::activity1-input1", result);
}
Use TestWorkflowEnvironment to test a workflow code.-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()<T> TnewActivityStub(Class<T> activityInterface) Creates a stub that can be used to invoke activities registered throughregisterActivitiesImplementations(Object...).<T> TnewActivityStub(Class<T> activityInterface, io.temporal.activity.ActivityOptions options) Creates a stub that can be used to invoke activities registered throughregisterActivitiesImplementations(Object...).static TestActivityEnvironmentstatic TestActivityEnvironmentnewInstance(TestEnvironmentOptions options) <T> TnewLocalActivityStub(Class<T> activityInterface, io.temporal.activity.LocalActivityOptions options, Map<String, io.temporal.activity.LocalActivityOptions> activityMethodOptions) Creates a stub that can be used to invoke activities registered throughregisterActivitiesImplementations(Object...).voidregisterActivitiesImplementations(Object... activityImplementations) Registers activity implementations to test.voidRequests activity cancellation.<T> voidsetActivityHeartbeatListener(Class<T> detailsClass, io.temporal.workflow.Functions.Proc1<T> listener) Sets a listener that is called every time an activity implementation heartbeats throughActivityExecutionContext.heartbeat(Object).<T> voidsetActivityHeartbeatListener(Class<T> detailsClass, Type detailsType, io.temporal.workflow.Functions.Proc1<T> listener) Sets a listener that is called every time an activity implementation heartbeats throughActivityExecutionContext.heartbeat(Object).<T> voidsetHeartbeatDetails(T details) Sets heartbeat details for the next activity execution.
-
Method Details
-
newInstance
-
newInstance
-
registerActivitiesImplementations
Registers activity implementations to test. UsenewActivityStub(Class)to create stubs that can be used to invoke them.Implementations that share a worker must implement different interfaces as an activity type is identified by the activity interface, not by the implementation.
- Throws:
io.temporal.worker.TypeAlreadyRegisteredException- if one of the activity types is already registered
-
newActivityStub
Creates a stub that can be used to invoke activities registered throughregisterActivitiesImplementations(Object...).Activity methods may throw
ActivityRequestedAsyncCompletionif the activity requested async completion.- Type Parameters:
T- Type of the activity interface.- Parameters:
activityInterface- activity interface class that the object under test implements.- Returns:
- The stub that implements the activity interface.
-
newActivityStub
Creates a stub that can be used to invoke activities registered throughregisterActivitiesImplementations(Object...).Activity methods may throw
ActivityRequestedAsyncCompletionif the activity requested async completion.- Type Parameters:
T- Type of the activity interface.- Parameters:
activityInterface- activity interface class that the object under test implementsoptions- options that specify the activity invocation parameters- Returns:
- The stub that implements the activity interface.
-
newLocalActivityStub
<T> T newLocalActivityStub(Class<T> activityInterface, io.temporal.activity.LocalActivityOptions options, Map<String, io.temporal.activity.LocalActivityOptions> activityMethodOptions) Creates a stub that can be used to invoke activities registered throughregisterActivitiesImplementations(Object...).- Type Parameters:
T- Type of the activity interface.- Parameters:
activityInterface- activity interface class that the object under test implementsoptions- options that specify the activity invocation parametersactivityMethodOptions- a map keyed on Activity Type Name to its specific invocation parameters. By default the name of an activity type is its method name with the first letter capitalized.- Returns:
- The stub that implements the activity interface.
-
setActivityHeartbeatListener
<T> void setActivityHeartbeatListener(Class<T> detailsClass, io.temporal.workflow.Functions.Proc1<T> listener) Sets a listener that is called every time an activity implementation heartbeats throughActivityExecutionContext.heartbeat(Object).- Type Parameters:
T- Type of the heartbeat details.- Parameters:
detailsClass- class of the details passed to theActivityExecutionContext.heartbeat(Object).listener- listener to register.
-
setActivityHeartbeatListener
<T> void setActivityHeartbeatListener(Class<T> detailsClass, Type detailsType, io.temporal.workflow.Functions.Proc1<T> listener) Sets a listener that is called every time an activity implementation heartbeats throughActivityExecutionContext.heartbeat(Object).- Type Parameters:
T- Type of the heartbeat details.- Parameters:
detailsClass- class of the details passed to theActivityExecutionContext.heartbeat(Object).detailsType- type of the details. Differs for detailsClass for generic types.listener- listener to register.
-
setHeartbeatDetails
<T> void setHeartbeatDetails(T details) Sets heartbeat details for the next activity execution. The next activity called from this TestActivityEnvironment will be able to access this value usingActivityExecutionContext.getHeartbeatDetails(Class). This value is cleared upon execution.- Type Parameters:
T- Type of the heartbeat details.- Parameters:
details- The details object to make available to the next activity call.
-
requestCancelActivity
void requestCancelActivity()Requests activity cancellation. The cancellation is going to be delivered to the activity on the next heartbeat. -
close
void close()
-