temporalio / sdk-java

Temporal Java SDK
https://temporal.io
Apache License 2.0
199 stars 134 forks source link

TestActivityEnvironment does not work with Asynchronous Activity Completion #2097

Open bjab opened 3 weeks ago

bjab commented 3 weeks ago

We have activity, that is using "Asynchronous Activity Completion" and we want to test it. In our case, we just want to test, that activity will send proper kafka message, but still, we need to set up test environment, to obtain activity token, etc.

In shortest form, our test looks like this:

var testActivityEnvironment = TestActivityEnvironment.newInstance(TestEnvironmentOptions.getDefaultInstance());
testActivityEnvironment.registerActivitiesImplementations(activityImpl);
var testActivity = testActivityEnvironment.newActivityStub(SomeActivities.class);

testActivity.execute();

Inside of activityImpl we use context.doNotCompleteOnReturn();

Problem is, that TestActivityEnvironmentInternal expects either cancelled, failed or completed activity. In case of not async activity, all fields of ActivityTaskHandler.Result are null and we get NullPointerException

Problematic block of code: https://github.com/temporalio/sdk-java/blob/master/temporal-testing/src/main/java/io/temporal/testing/TestActivityEnvironmentInternal.java#L489-L496

Expected Behavior

Calling mehod of async activity will do nothing.

Actual Behavior

NullPointerException at https://github.com/temporalio/sdk-java/blob/master/temporal-testing/src/main/java/io/temporal/testing/TestActivityEnvironmentInternal.java#L496 (response.getTaskFailed() is null)

Specifications