Java - how to unit test remote activities that are hosted in different applications without access to Workflow code

I have a workflow that has 4 activities basically. 2 Of them are local where as the other 2 are remote micro services.

So if I have to write Junits for the activity implementation in the micro services, how do I do it?
My remote activity implementation is as below:

public Class CalculationActivityImpl implements CalculationActivity {

  
  String determineParameters(Long runId, String filePath) {
        ... do some stuff
  ActivityInfo activityInfo = Activity.getExecutionContext().getInfo();
  WorkflowStub runWorkflow = workflowClient.newUntypedWorkflowStub(
        activityInfo.getWorkflowId(),
        Optional.of(activityInfo.getRunId()), Optional.of(activityInfo.getWorkflowType()));
    runWorkflow.signal("signalCalculationDone");
  }

 return "done";
}

So here am using UntypedWorkflowStub to signal workflow. I went through multiple samples in sdk-java but hardly I could find references on how to unit test remotely hosted activities.

For now I could only come up with this by referring to TestActivityEnvironment Javadocs.

@Test
  public void testCalculation() {
    TestActivityEnvironment testActivityEnv = TestActivityEnvironment.newInstance();
    testActivityEnv.registerActivitiesImplementations(new CalculationActivityImpl());
    CalculationActivity activity = testActivityEnv.newActivityStub(CalculationActivity.class);
    String output = activity.determineParameters(1L, "/CalculationResults");

    assertEquals("done", output);
  }

But this is failing with ERROR TestActivityEnvironmentInternal: Timeout trying execute activity task task_token: “test-task-token”. Apparently, it’s getting timed out. Without workflow context, is it really possible to unit test activities?

Any references or pointers would be much appreciated.

Are you injecting your workflowClient in the activity?
For unit testing CalculationActivityImpl you should probably have a test configuration with a producer that produces a mocked WorkflowClient rather than using a real one.

No, in my activity class, I get workflowClient using below code. WorkflowClient.newInstance(WorkflowServiceStubs.newInstance()).

Here my scenario is I have an application with temporal workflow. It basically has 4 activities to do, 2 of which are bundled in the same application where as the other 2 are independent spark microservices.

Now since I will not have the workflow interface and impl code in my spark microservice, how can I get the activity tested. One solution I could think of is to add dummy workflow interface and impl class in my test package and test it like this: samples-java/TripBookingWorkflowTest.java at main · temporalio/samples-java · GitHub

Are there any other alternatives? is it possible to unit test activities alone without the TestWorkflowEnvironment.

how can I get the activity tested

If you do not want to mock the activity as you mentioned, and want to write an integration test where you want to use an external Temporal service rather than the in-memory test one, you can set TestEnvironmentOptions-> setUseExternalService to true. (and set a target if its not the default 127.0.0.1:7233).

For unit testing your activity I would go with the mock approach as you mentioned.