|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved |
| 3 | + * |
| 4 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 9 | + * use this file except in compliance with the License. A copy of the License is |
| 10 | + * located at |
| 11 | + * |
| 12 | + * http://aws.amazon.com/apache2.0 |
| 13 | + * |
| 14 | + * or in the "license" file accompanying this file. This file is distributed on |
| 15 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 16 | + * express or implied. See the License for the specific language governing |
| 17 | + * permissions and limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.temporal.samples.hello; |
| 21 | + |
| 22 | +import static org.mockito.Mockito.*; |
| 23 | + |
| 24 | +import io.temporal.testing.TestWorkflowEnvironment; |
| 25 | +import io.temporal.testing.TestWorkflowExtension; |
| 26 | +import io.temporal.worker.Worker; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 31 | + |
| 32 | +/** Unit test for {@link HelloChild}. Doesn't use an external Temporal service. */ |
| 33 | +public class HelloChildJUnit5Test { |
| 34 | + |
| 35 | + @RegisterExtension |
| 36 | + public static final TestWorkflowExtension testWorkflowExtension = |
| 37 | + TestWorkflowExtension.newBuilder() |
| 38 | + .setWorkflowTypes(HelloChild.GreetingWorkflowImpl.class) |
| 39 | + .setDoNotStart(true) |
| 40 | + .build(); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testMockedChild( |
| 44 | + TestWorkflowEnvironment testEnv, Worker worker, HelloChild.GreetingWorkflow workflow) { |
| 45 | + |
| 46 | + // As new mock is created on each workflow task the only last one is useful to verify calls. |
| 47 | + AtomicReference<HelloChild.GreetingChild> lastChildMock = new AtomicReference<>(); |
| 48 | + // Factory is called to create a new workflow object on each workflow task. |
| 49 | + worker.addWorkflowImplementationFactory( |
| 50 | + HelloChild.GreetingChild.class, |
| 51 | + () -> { |
| 52 | + HelloChild.GreetingChild child = mock(HelloChild.GreetingChild.class); |
| 53 | + when(child.composeGreeting("Hello", "World")).thenReturn("Bye World!"); |
| 54 | + lastChildMock.set(child); |
| 55 | + return child; |
| 56 | + }); |
| 57 | + |
| 58 | + testEnv.start(); |
| 59 | + |
| 60 | + // Execute a workflow waiting for it to complete. |
| 61 | + String greeting = workflow.getGreeting("World"); |
| 62 | + Assert.assertEquals("Bye World!", greeting); |
| 63 | + HelloChild.GreetingChild mock = lastChildMock.get(); |
| 64 | + verify(mock).composeGreeting(eq("Hello"), eq("World")); |
| 65 | + |
| 66 | + testEnv.shutdown(); |
| 67 | + } |
| 68 | +} |
0 commit comments