Skip to content

Commit

Permalink
SpringBoot test with mocked activity
Browse files Browse the repository at this point in the history
Signed-off-by: Tihomir Surdilovic <[email protected]>
  • Loading branch information
tsurdilo committed Jun 30, 2024
1 parent d994600 commit 1f79f30
Showing 1 changed file with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
*
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package io.temporal.samples.springboot;

import static org.mockito.ArgumentMatchers.any;

import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.samples.springboot.hello.HelloActivity;
import io.temporal.samples.springboot.hello.HelloActivityImpl;
import io.temporal.samples.springboot.hello.HelloWorkflow;
import io.temporal.samples.springboot.hello.model.Person;
import io.temporal.testing.TestWorkflowEnvironment;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.util.Assert;

@SpringBootTest(classes = HelloSampleTestMockedActivity.Configuration.class)
@ActiveProfiles("test")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
// set this to omit setting up embedded kafka
@EnableAutoConfiguration(exclude = {KafkaAutoConfiguration.class})
@DirtiesContext
public class HelloSampleTestMockedActivity {

@Autowired ConfigurableApplicationContext applicationContext;

@Autowired TestWorkflowEnvironment testWorkflowEnvironment;

@Autowired WorkflowClient workflowClient;

@Captor ArgumentCaptor<Person> personArgumentCaptor;

@Autowired HelloActivity activity;

@BeforeEach
void setUp() {
applicationContext.start();
}

@Test
public void testHello() {
HelloWorkflow workflow =
workflowClient.newWorkflowStub(
HelloWorkflow.class,
WorkflowOptions.newBuilder()
.setTaskQueue("HelloSampleTaskQueue")
.setWorkflowId("HelloSampleTest")
.build());
String result = workflow.sayHello(new Person("Temporal", "User"));
Assert.notNull(result, "Greeting should not be null");
Assert.isTrue(result.equals("Hello from mocked activity"), "Invalid result");

Mockito.verify(activity, Mockito.times(1)).hello(personArgumentCaptor.capture());
Assert.notNull(personArgumentCaptor.getValue(), "Invalid input");
Assert.isTrue(
personArgumentCaptor.getValue().getFirstName().equals("Temporal"),
"Invalid person first name");
Assert.isTrue(
personArgumentCaptor.getValue().getLastName().equals("User"), "invalid person last name");
}

@ComponentScan
public static class Configuration {
@MockBean private HelloActivityImpl helloActivityMock;

@Bean
@Primary
public HelloActivity getTestActivityImpl() {
Mockito.when(helloActivityMock.hello(any())).thenReturn("Hello from mocked activity");
return helloActivityMock;
}
}
}

0 comments on commit 1f79f30

Please sign in to comment.