8000 SpringBoot test with mocked activity (#637) · temporalio/samples-java@051b925 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 051b925

Browse files
authored
SpringBoot test with mocked activity (#637)
Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent d994600 commit 051b925

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.springboot;
21+
22+
import static org.mockito.ArgumentMatchers.any;
23+
24+
import io.temporal.client.WorkflowClient;
25+
import io.temporal.client.WorkflowOptions;
26+
import io.temporal.samples.springboot.hello.HelloActivity;
27+
import io.temporal.samples.springboot.hello.HelloActivityImpl;
28+
import io.temporal.samples.springboot.hello.HelloWorkflow;
29+
import io.temporal.samples.springboot.hello.model.Person;
30+
import io.temporal.testing.TestWorkflowEnvironment;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.TestInstance;
34+
import org.mockito.ArgumentCaptor;
35+
import org.mockito.Captor;
36+
import org.mockito.Mockito;
37+
import org.springframework.beans.factory.annotation.Autowired;
38+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
39+
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
40+
import org.springframework.boot.test.context.SpringBootTest;
41+
import org.springframework.boot.test.mock.mockito.MockBean;
42+
import org.springframework.context.ConfigurableApplicationContext;
43+
import org.springframework.context.annotation.Bean;
44+
import org.springframework.context.annotation.ComponentScan;
45+
import org.springframework.context.annotation.Primary;
46+
import org.springframework.test.annotation.DirtiesContext;
47+
import org.springframework.test.context.ActiveProfiles;
48+
import org.springframework.util.Assert;
49+
50+
@SpringBootTest(classes = HelloSampleTestMockedActivity.Configuration.class)
51+
@ActiveProfiles("test")
52+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
53+
// set this to omit setting up embedded kafka
54+
@EnableAutoConfiguration(exclude = {KafkaAutoConfiguration.class})
55+
@DirtiesContext
56+
public class HelloSampleTestMockedActivity {
57+
58+
@Autowired ConfigurableApplicationContext applicationContext;
59+
60+
@Autowired TestWorkflowEnvironment testWorkflowEnvironment;
61+
62+
@Autowired WorkflowClient workflowClient;
63+
64+
@Captor ArgumentCaptor<Person> personArgumentCaptor;
65+
66+
@Autowired HelloActivity activity;
67+
68+
@BeforeEach
69+
void setUp() {
70+
applicationContext.start();
71+
}
72+
73+
@Test
74+
public void testHello() {
75+
HelloWorkflow workflow =
76+
workflowClient.newWorkflowStub(
77+
HelloWorkflow.class,
78+
WorkflowOptions.newBuilder()
79+
.setTaskQueue("HelloSampleTaskQueue")
80+
.setWorkflowId("HelloSampleTest")
81+
.build());
82+
String result = workflow.sayHello(new Person("Temporal", "User"));
83+
Assert.notNull(result, "Greeting should not be null");
84+
Assert.isTrue(result.equals("Hello from mocked activity"), "Invalid result");
85+
86+
Mockito.verify(activity, Mockito.times(1)).hello(personArgumentCaptor.capture());
87+
Assert.notNull(personArgumentCaptor.getValue(), "Invalid input");
88+
Assert.isTrue(
89+
personArgumentCaptor.getValue().getFirstName().equals("Temporal"),
90+
"Invalid person first name");
91+
Assert.is 884E True(
92+
personArgumentCaptor.getValue().getLastName().equals("User"), "invalid person last name");
93+
}
94+
95+
@ComponentScan
96+
public static class Configuration {
97+
@MockBean private HelloActivityImpl helloActivityMock;
98+
99+
@Bean
100+
@Primary
101+
public HelloActivity getTestActivityImpl() {
102+
Mockito.when(helloActivityMock.hello(any())).thenReturn("Hello from mocked activity");
103+
return helloActivityMock;
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)
0