|
| 1 | +package io.temporal.samples.nexus.caller; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | + |
| 7 | +import io.nexusrpc.handler.OperationHandler; |
| 8 | +import io.temporal.samples.nexus.handler.NexusServiceImpl; |
| 9 | +import io.temporal.samples.nexus.service.NexusService; |
| 10 | +import io.temporal.testing.TestWorkflowEnvironment; |
| 11 | +import io.temporal.testing.TestWorkflowExtension; |
| 12 | +import io.temporal.worker.Worker; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 15 | + |
| 16 | +// This unit test example shows how to mock the Nexus service itself in JUnit4. |
| 17 | +// In this example since the NexusService itself is mocked, no handlers need to be set up or mocked. |
| 18 | + |
| 19 | +public class NexusServiceJunit5Test { |
| 20 | + |
| 21 | + private final NexusServiceImpl mockNexusService = createMockNexusService(); |
| 22 | + |
| 23 | + // Mutable fields — set these in each test method before starting the environment |
| 24 | + // to mock the return values for the Nexus service. |
| 25 | + // Instance fields (not static) so each test gets its own copy; safe for parallel execution. |
| 26 | + private NexusService.EchoOutput echoResult = new NexusService.EchoOutput("default"); |
| 27 | + private NexusService.HelloOutput helloResult = new NexusService.HelloOutput("default"); |
| 28 | + |
| 29 | + private NexusServiceImpl createMockNexusService() { |
| 30 | + NexusServiceImpl mock = mock(NexusServiceImpl.class); |
| 31 | + // Using OperationHandler.sync for both operations bypasses the need for a backing workflow, |
| 32 | + // returning results inline just like a synchronous call. Mocks need to be done before the rule |
| 33 | + // is defined, as creating the rule will fail if either call is still null. |
| 34 | + // |
| 35 | + // The following is the simplest - just mock the services to return a value. But then these |
| 36 | + // values cannot change per test case, so this will not always suffice. |
| 37 | + // when(mock.echo()) |
| 38 | + // .thenReturn( |
| 39 | + // OperationHandler.sync( |
| 40 | + // (ctx, details, input) -> new NexusService.EchoOutput("mocked echo"))); |
| 41 | + // when(mock.hello()) |
| 42 | + // .thenReturn( |
| 43 | + // OperationHandler.sync( |
| 44 | + // (ctx, details, input) -> new NexusService.HelloOutput("Hello Mock World |
| 45 | + // 👋"))); |
| 46 | + // |
| 47 | + // An alternative approach is to create the mocks but set them to return a value that is set in |
| 48 | + // the unit |
| 49 | + // test class above. That allows you to change the return value in each test. |
| 50 | + when(mock.echo()).thenReturn(OperationHandler.sync((ctx, details, input) -> echoResult)); |
| 51 | + when(mock.hello()).thenReturn(OperationHandler.sync((ctx, details, input) -> helloResult)); |
| 52 | + |
| 53 | + return mock; |
| 54 | + } |
| 55 | + |
| 56 | + @RegisterExtension |
| 57 | + public final TestWorkflowExtension testWorkflowExtension = |
| 58 | + TestWorkflowExtension.newBuilder() |
| 59 | + // If a Nexus service is registered as part of the test as in the following line of code, |
| 60 | + // the TestWorkflowExtension will, by default, automatically create a Nexus service |
| 61 | + // endpoint and workflows registered as part of the TestWorkflowExtension will |
| 62 | + // automatically inherit the endpoint if none is set. |
| 63 | + .setNexusServiceImplementation(mockNexusService) |
| 64 | + // The Echo Nexus handler service just makes a call to a class, so no extra setup is |
| 65 | + // needed. But the Hello Nexus service needs a worker for both the caller and handler |
| 66 | + // in order to run, and the Echo Nexus caller service needs a worker. |
| 67 | + // |
| 68 | + // registerWorkflowImplementationTypes will take the classes given and create workers for |
| 69 | + // them, enabling workflows to run. |
| 70 | + // Since both operations are mocked with OperationHandler.sync, no backing workflow is |
| 71 | + // needed for hello — only the caller workflow types need to be registered. |
| 72 | + .registerWorkflowImplementationTypes( |
| 73 | + HelloCallerWorkflowImpl.class, EchoCallerWorkflowImpl.class) |
| 74 | + // The workflow will start before each test, and will shut down after each test. |
| 75 | + // See CallerWorkflowTest for an example of how to control this differently if needed. |
| 76 | + .build(); |
| 77 | + |
| 78 | + // The TestWorkflowExtension extension in the Temporal testing library creates the |
| 79 | + // arguments to the test cases and initializes them from the extension setup call above. |
| 80 | + @Test |
| 81 | + public void testHelloWorkflow( |
| 82 | + TestWorkflowEnvironment testEnv, Worker worker, HelloCallerWorkflow workflow) { |
| 83 | + // Set the mock value to return |
| 84 | + helloResult = new NexusService.HelloOutput("Hello Mock World 👋"); |
| 85 | + // Execute a workflow waiting for it to complete. |
| 86 | + String greeting = workflow.hello("World", NexusService.Language.EN); |
| 87 | + assertEquals("Hello Mock World 👋", greeting); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testEchoWorkflow( |
| 92 | + TestWorkflowEnvironment testEnv, Worker worker, EchoCallerWorkflow workflow) { |
| 93 | + // Set the mock value to return |
| 94 | + echoResult = new NexusService.EchoOutput("mocked echo"); |
| 95 | + // Execute a workflow waiting for it to complete. |
| 96 | + String greeting = workflow.echo("Hello"); |
| 97 | + assertEquals("mocked echo", greeting); |
| 98 | + } |
| 99 | +} |
0 commit comments