Hi Temporal team, I am having some trouble unit testing a Workflow class. I’m using Spring Boot, Junit 5, and Mockito.
This is my Workflow class:
@Component
@WorkflowImpl(taskQueues = "TaskQueue")
public class UpgradeInstanceWorkflowImpl implements UpgradeInstanceWorkflow {
@Autowired Util util;
@Override
public String beginUpgradeInstance(UpgradeRequest upgradeRequest) {
// Configure activity
long activityTimeout = 0;
activityTimeout = util.getConfigurationCheckTimeout(temporalActivityTimeoutConfig);
UpgradeInstanceActivity upgradeInstanceActivity =
Workflow.newActivityStub(
UpgradeInstanceActivity.class,
ActivityOptions.newBuilder()
.setScheduleToStartTimeout(Duration.ofMinutes(10))
.setStartToCloseTimeout(Duration.ofHours(activityTimeout))
.setRetryOptions(upgradeRetryOptions)
.build());
upgradeInstanceActivity.backupJenkinsMasterActivity(upgradeRequest);
}
As you can see I have an @Autowired Util util;
variable because I need to use a function from this class.
My unit test looks like this:
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
@ContextConfiguration(classes = {TemporalTestConfig.class})
@Profile("test")
public class UpgradeInstanceWorkflowTest {
@MockBean Util util;
@Autowired UpgradeInstanceActivity activity;
@RegisterExtension
public final TestWorkflowExtension testWorkflowExtension =
TestWorkflowExtension.newBuilder()
.setWorkflowTypes(UpgradeInstanceWorkflowImpl.class)
.setDoNotStart(true)
.build();
@Test
public void testBeginUpgradeInstance(
TestWorkflowEnvironment testWorkflowEnvironment,
Worker worker,
UpgradeInstanceWorkflow workflow)
throws InterruptedException {
// Mock activities
UpgradeInstanceActivity activities =
mock(activity.getClass(), withSettings().withoutAnnotations());
doNothing().when(activities).backupJenkinsMasterActivity(any());
doNothing().when(activities).scheduleUpgradeActivity(any());
doNothing().when(activities).checkUpgradeRequestStatusActivity(any());
// Register with worker
worker.registerActivitiesImplementations(activities);
// Start test environment
testWorkflowEnvironment.start();
int workflowTimeout = 8;
doReturn(workflowTimeout).when(util).getConfigurationCheckTimeout(any());
String result = workflow.beginUpgradeInstance(upgradeRequest);
assertEquals("Upgrade Successful", result);
}
}
This is the config class I’ve defined:
public class TemporalTestConfig {
private static final Logger logger = LoggerFactory.getLogger(TemporalTestConfig.class);
private static TestWorkflowEnvironment testWorkflowEnvironment =
TestWorkflowEnvironment.newInstance();
private static TestActivityEnvironment testActivityEnvironment =
TestActivityEnvironment.newInstance();
@Bean
public TestWorkflowEnvironment testWorkflowEnvironment() {
return testWorkflowEnvironment;
}
@Bean
public WorkflowClient workflowClient() {
return testWorkflowEnvironment.getWorkflowClient();
}
}
However, I am getting this exception:
Caused by: java.lang.NullPointerException: Cannot invoke "com.nvidia.ipp.jenkinsupgradeprocess.util.Util.getConfigurationCheckTimeout(String)" because "this.util" is null
even though I have injected the @MockBean Util util. It’s making me think that the wrong Workflow Bean is getting called, but I haven’t been able to get this working.
How can I fix this issue? Thanks in advance.