-
Notifications
You must be signed in to change notification settings - Fork 182
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 2 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/EchoHandler.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 EchoHandler { | ||
| NexusService.EchoOutput echo(NexusService.EchoInput input); | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
core/src/main/java/io/temporal/samples/nexus/handler/EchoHandlerImpl.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 EchoHandlerImpl implements EchoHandler { | ||
| @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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
| 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.EchoHandler; | ||
| 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; | ||
|
|
||
| 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 EchoHandler mockEchoHandler = mock(EchoHandler.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(mockEchoHandler)) | ||
|
Evanthx marked this conversation as resolved.
Outdated
|
||
| // 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 EchoHandler dependency directly. | ||
|
Evanthx marked this conversation as resolved.
Outdated
|
||
| when(mockEchoHandler.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(); | ||
| } | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
| 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; | ||
|
|
||
| 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); | ||
| } | ||
| } |
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
| 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.EchoHandler; | ||
| 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; | ||
|
|
||
| public class CallerWorkflowMockTest { | ||
|
|
||
| // Inject a mock EchoHandler 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 EchoHandler mockEchoHandler = mock(EchoHandler.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(mockEchoHandler)) | ||
| // 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 EchoHandler dependency directly. | ||
| when(mockEchoHandler.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,9 @@ | ||
| 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.HelloHandlerWorkflow; | ||
| 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.TestWorkflowRule; | ||
|
|
@@ -21,28 +18,26 @@ public class CallerWorkflowTest { | |
| @Rule | ||
| public TestWorkflowRule testWorkflowRule = | ||
| TestWorkflowRule.newBuilder() | ||
| // If a Nexus service is registered as part of the test, 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. | ||
| // 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()) | ||
| .setWorkflowTypes(HelloCallerWorkflowImpl.class) | ||
| // 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 is not adding an EchoCallerWorkflow though - | ||
| // see the testEchoWorkflow test method below for an example of an alternate way | ||
| // to supply a worker that gives you more flexibility if needed. | ||
| .setWorkflowTypes(HelloCallerWorkflowImpl.class, HelloHandlerWorkflowImpl.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 World 👋")); | ||
| return wf; | ||
| }); | ||
| testWorkflowRule.getTestEnvironment().start(); | ||
|
|
||
| HelloCallerWorkflow workflow = | ||
|
|
@@ -61,8 +56,13 @@ public void testHelloWorkflow() { | |
| public void testEchoWorkflow() { | ||
| // If Workflows are registered later than the endpoint can be set manually | ||
| // either by setting the endpoint in the NexusServiceOptions in the Workflow implementation or | ||
| // by setting the NexusServiceOptions on the WorkflowImplementationOptions when registering the | ||
| // Workflow. | ||
| // by setting the NexusServiceOptions on the WorkflowImplementationOptions when registering | ||
| // the Workflow. To demonstrate, this is creating the Nexus service for Echo, | ||
| // and registering a EchoCallerWorkflowImpl worker. | ||
| // | ||
| // It is much simpler to use the setWorkflowTypes in the rule definition above - and as | ||
| // this isn't easily do-able in JUnit5 (the nexus endpoint isn't exposed) should be | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this an SDK issue? |
||
| // used with caution. | ||
| testWorkflowRule | ||
| .getWorker() | ||
| .registerWorkflowImplementationTypes( | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.