-
Notifications
You must be signed in to change notification settings - Fork 183
Code samples for testing and mocking Nexus #771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
47da04e
Code samples for testing and mocking Nexus
Evanthx 70c21bb
Formatting changes from Spotless
Evanthx 66487a9
Added two more classes that mock the Nexus Service itself
Evanthx 9dded5d
Renamed EchoHandler to EchoClient
Evanthx de0a9bb
Updating gradle wrapper validator
Evanthx 5dc119b
Changed NexusService name for clarity, modified two tests
Evanthx 75b5fed
Suppressing two false warnings that turned into build errors
Evanthx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
core/src/main/java/io/temporal/samples/nexus/handler/EchoClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package io.temporal.samples.nexus.handler; | ||
|
|
||
| import io.temporal.samples.nexus.service.NexusService; | ||
|
|
||
| public interface EchoClient { | ||
| NexusService.EchoOutput echo(NexusService.EchoInput input); | ||
| } |
12 changes: 12 additions & 0 deletions
12
core/src/main/java/io/temporal/samples/nexus/handler/EchoClientImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.temporal.samples.nexus.handler; | ||
|
|
||
| import io.temporal.samples.nexus.service.NexusService; | ||
|
|
||
| // Note that this is a class, not a Temporal worker. This is to demonstrate that Nexus services can | ||
| // simply call a class instead of a worker for fast operations that don't need retry handling. | ||
| public class EchoClientImpl implements EchoClient { | ||
| @Override | ||
| public NexusService.EchoOutput echo(NexusService.EchoInput input) { | ||
| return new NexusService.EchoOutput(input.getMessage()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
core/src/test/java/io/temporal/samples/nexus/caller/CallerWorkflowJunit5MockTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package io.temporal.samples.nexus.caller; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import io.temporal.samples.nexus.handler.EchoClient; | ||
| import io.temporal.samples.nexus.handler.HelloHandlerWorkflow; | ||
| import io.temporal.samples.nexus.handler.NexusServiceImpl; | ||
| import io.temporal.samples.nexus.service.NexusService; | ||
| import io.temporal.testing.TestWorkflowEnvironment; | ||
| import io.temporal.testing.TestWorkflowExtension; | ||
| import io.temporal.worker.Worker; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| // This is an example of how to unit test Nexus services in JUnit5. The handlers are mocked, | ||
| // so that the caller classes interact with the mocks and not the handler classes themselves. | ||
|
|
||
| public class CallerWorkflowJunit5MockTest { | ||
|
|
||
| // Sync Nexus operations run inline in the handler thread — there is no backing workflow to | ||
| // register a factory for. To mock one, inject a mock dependency into the service implementation. | ||
| private static final EchoClient mockEchoClient = mock(EchoClient.class); | ||
|
|
||
| @RegisterExtension | ||
| public static final TestWorkflowExtension testWorkflowExtension = | ||
| TestWorkflowExtension.newBuilder() | ||
| // If a Nexus service is registered as part of the test as in the following line of code, | ||
| // the TestWorkflowExtension will, by default, automatically create a Nexus service | ||
| // endpoint and workflows registered as part of the TestWorkflowExtension will | ||
| // automatically inherit the endpoint if none is set. | ||
| .setNexusServiceImplementation(new NexusServiceImpl(mockEchoClient)) | ||
| // The Echo Nexus handler service just makes a call to a class, so no extra setup is | ||
| // needed. But the Hello Nexus service needs a worker for both the caller and handler | ||
| // in order to run, and the Echo Nexus caller service needs a worker. | ||
| // | ||
| // registerWorkflowImplementationTypes will take the classes given and create workers for | ||
| // them, enabling workflows to run. | ||
| .registerWorkflowImplementationTypes( | ||
| HelloCallerWorkflowImpl.class, EchoCallerWorkflowImpl.class) | ||
| .setDoNotStart(true) | ||
| .build(); | ||
|
|
||
| @Test | ||
| public void testHelloWorkflow( | ||
| TestWorkflowEnvironment testEnv, Worker worker, HelloCallerWorkflow workflow) { | ||
| // Workflows started by a Nexus service can be mocked just like any other workflow | ||
| worker.registerWorkflowImplementationFactory( | ||
| HelloHandlerWorkflow.class, | ||
| () -> { | ||
| HelloHandlerWorkflow mockHandler = mock(HelloHandlerWorkflow.class); | ||
| when(mockHandler.hello(any())) | ||
| .thenReturn(new NexusService.HelloOutput("Hello Mock World 👋")); | ||
| return mockHandler; | ||
| }); | ||
| testEnv.start(); | ||
|
|
||
| // Execute a workflow waiting for it to complete. | ||
| String greeting = workflow.hello("World", NexusService.Language.EN); | ||
| assertEquals("Hello Mock World 👋", greeting); | ||
|
|
||
| testEnv.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEchoWorkflow( | ||
| TestWorkflowEnvironment testEnv, Worker worker, EchoCallerWorkflow workflow) { | ||
| // Sync Nexus operations run inline in the handler thread — there is no backing workflow to | ||
| // register a factory for. Instead, stub the injected EchoClient dependency directly. | ||
| when(mockEchoClient.echo(any())).thenReturn(new NexusService.EchoOutput("mocked echo")); | ||
| testEnv.start(); | ||
|
|
||
| // Execute a workflow waiting for it to complete. | ||
| String greeting = workflow.echo("Hello"); | ||
| assertEquals("mocked echo", greeting); | ||
|
|
||
| testEnv.shutdown(); | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
core/src/test/java/io/temporal/samples/nexus/caller/CallerWorkflowJunit5Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package io.temporal.samples.nexus.caller; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import io.temporal.samples.nexus.handler.HelloHandlerWorkflowImpl; | ||
| import io.temporal.samples.nexus.handler.NexusServiceImpl; | ||
| import io.temporal.samples.nexus.service.NexusService; | ||
| import io.temporal.testing.TestWorkflowEnvironment; | ||
| import io.temporal.testing.TestWorkflowExtension; | ||
| import io.temporal.worker.Worker; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| // This is an example of how to unit test Nexus services in JUnit5. The handlers are not mocked, | ||
| // but are actually called by the testing framework by the caller classes. | ||
|
|
||
| public class CallerWorkflowJunit5Test { | ||
|
|
||
| @RegisterExtension | ||
| public static final TestWorkflowExtension testWorkflowExtension = | ||
| TestWorkflowExtension.newBuilder() | ||
| // If a Nexus service is registered as part of the test as in the following line of code, | ||
| // the TestWorkflowExtension will, by default, automatically create a Nexus service | ||
| // endpoint and workflows registered as part of the TestWorkflowExtension will | ||
| // automatically inherit the endpoint if none is set. | ||
| .setNexusServiceImplementation(new NexusServiceImpl()) | ||
| // The Echo Nexus handler service just makes a call to a class, so no extra setup is | ||
| // needed. But the Hello Nexus service needs a worker for both the caller and handler | ||
| // in order to run, and the Echo Nexus caller service needs a worker. | ||
| // | ||
| // registerWorkflowImplementationTypes will take the classes given and create workers for | ||
| // them, enabling workflows to run. | ||
| .registerWorkflowImplementationTypes( | ||
| HelloCallerWorkflowImpl.class, | ||
| HelloHandlerWorkflowImpl.class, | ||
| EchoCallerWorkflowImpl.class) | ||
| // The workflow will start before each test, and will shut down after each test. | ||
| // See CallerWorkflowTest for an example of how to control this differently if needed. | ||
| .build(); | ||
|
|
||
| // The TestWorkflowExtension extension in the Temporal testing library creates the | ||
| // arguments to the test cases and initializes them from the extension setup call above. | ||
| @Test | ||
| public void testHelloWorkflow( | ||
| TestWorkflowEnvironment testEnv, Worker worker, HelloCallerWorkflow workflow) { | ||
| // Execute a workflow waiting for it to complete. | ||
| String greeting = workflow.hello("World", NexusService.Language.EN); | ||
| assertEquals("Hello World 👋", greeting); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEchoWorkflow( | ||
| TestWorkflowEnvironment testEnv, Worker worker, EchoCallerWorkflow workflow) { | ||
| // Execute a workflow waiting for it to complete. | ||
| String greeting = workflow.echo("Hello"); | ||
| assertEquals("Hello", greeting); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
core/src/test/java/io/temporal/samples/nexus/caller/CallerWorkflowMockTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package io.temporal.samples.nexus.caller; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import io.temporal.client.WorkflowOptions; | ||
| import io.temporal.samples.nexus.handler.EchoClient; | ||
| import io.temporal.samples.nexus.handler.HelloHandlerWorkflow; | ||
| import io.temporal.samples.nexus.handler.NexusServiceImpl; | ||
| import io.temporal.samples.nexus.service.NexusService; | ||
| import io.temporal.testing.TestWorkflowRule; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| // This is an example of how to unit test Nexus services in JUnit4. The handlers are mocked, | ||
| // so that the caller classes interact with the mocks and not the handler classes themselves. | ||
|
|
||
| public class CallerWorkflowMockTest { | ||
|
|
||
| // Inject a mock EchoClient so sync Nexus operations can be stubbed per test. | ||
| // JUnit 4 creates a new test class instance per test method, so this mock is fresh each time. | ||
| private final EchoClient mockEchoClient = mock(EchoClient.class); | ||
|
|
||
| @Rule | ||
| public TestWorkflowRule testWorkflowRule = | ||
| TestWorkflowRule.newBuilder() | ||
| // If a Nexus service is registered as part of the test as in the following line of code, | ||
| // the TestWorkflowRule will, by default, automatically create a Nexus service endpoint | ||
| // and workflows registered as part of the TestWorkflowRule | ||
| // will automatically inherit the endpoint if none is set. | ||
| .setNexusServiceImplementation(new NexusServiceImpl(mockEchoClient)) | ||
| // The Echo Nexus handler service just makes a call to a class, so no extra setup is | ||
| // needed. But the Hello Nexus service needs a worker for both the caller and handler | ||
| // in order to run. | ||
| // setWorkflowTypes will take the classes given and create workers for them, enabling | ||
| // workflows to run. This creates caller workflows, the handler workflows | ||
| // will be mocked in the test methods. | ||
| .setWorkflowTypes(HelloCallerWorkflowImpl.class, EchoCallerWorkflowImpl.class) | ||
| // Disable automatic worker startup as we are going to register some workflows manually | ||
| // per test | ||
| .setDoNotStart(true) | ||
| .build(); | ||
|
|
||
| @Test | ||
| public void testHelloWorkflow() { | ||
| testWorkflowRule | ||
| .getWorker() | ||
| // Workflows started by a Nexus service can be mocked just like any other workflow | ||
| .registerWorkflowImplementationFactory( | ||
| HelloHandlerWorkflow.class, | ||
| () -> { | ||
| HelloHandlerWorkflow wf = mock(HelloHandlerWorkflow.class); | ||
| when(wf.hello(any())).thenReturn(new NexusService.HelloOutput("Hello Mock World 👋")); | ||
| return wf; | ||
| }); | ||
| testWorkflowRule.getTestEnvironment().start(); | ||
|
|
||
| // Now create the caller workflow | ||
| HelloCallerWorkflow workflow = | ||
| testWorkflowRule | ||
| .getWorkflowClient() | ||
| .newWorkflowStub( | ||
| HelloCallerWorkflow.class, | ||
| WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.getTaskQueue()).build()); | ||
| String greeting = workflow.hello("World", NexusService.Language.EN); | ||
| assertEquals("Hello Mock World 👋", greeting); | ||
|
|
||
| testWorkflowRule.getTestEnvironment().shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEchoWorkflow() { | ||
| // Sync Nexus operations run inline in the handler thread — there is no backing workflow to | ||
| // register a factory for. Instead, stub the injected EchoCient dependency directly. | ||
| when(mockEchoClient.echo(any())).thenReturn(new NexusService.EchoOutput("mocked echo")); | ||
| testWorkflowRule.getTestEnvironment().start(); | ||
|
|
||
| EchoCallerWorkflow workflow = | ||
| testWorkflowRule | ||
| .getWorkflowClient() | ||
| .newWorkflowStub( | ||
| EchoCallerWorkflow.class, | ||
| WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.getTaskQueue()).build()); | ||
| String greeting = workflow.echo("Hello"); | ||
| assertEquals("mocked echo", greeting); | ||
|
|
||
| testWorkflowRule.getTestEnvironment().shutdown(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an SDK issue?