|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.bean.override.mockito; |
| 18 | + |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.Mockito; |
| 24 | + |
| 25 | +import org.springframework.aop.support.AopUtils; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.scheduling.annotation.Async; |
| 30 | +import org.springframework.scheduling.annotation.EnableAsync; |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 32 | + |
| 33 | +import static java.util.concurrent.CompletableFuture.completedFuture; |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.mockito.BDDMockito.given; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests for {@link MockitoBean @MockitoBean} where the mocked interface has an |
| 39 | + * {@link Async @Async} method. |
| 40 | + * |
| 41 | + * @author Sam Brannen |
| 42 | + * @author Andy Wilkinson |
| 43 | + * @since 6.2 |
| 44 | + */ |
| 45 | +@ExtendWith(SpringExtension.class) |
| 46 | +public class MockitoBeanAndAsyncInterfaceMethodTests { |
| 47 | + |
| 48 | + @MockitoBean |
| 49 | + Transformer transformer; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + MyService service; |
| 53 | + |
| 54 | + |
| 55 | + @Test |
| 56 | + void mockedMethodsAreNotAsync() throws Exception { |
| 57 | + assertThat(AopUtils.isAopProxy(transformer)).as("is Spring AOP proxy").isFalse(); |
| 58 | + assertThat(Mockito.mockingDetails(transformer).isMock()).as("is Mockito mock").isTrue(); |
| 59 | + |
| 60 | + given(transformer.transform("foo")).willReturn(completedFuture("bar")); |
| 61 | + assertThat(service.transform("foo")).isEqualTo("result: bar"); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + interface Transformer { |
| 66 | + |
| 67 | + @Async |
| 68 | + CompletableFuture<String> transform(String input); |
| 69 | + } |
| 70 | + |
| 71 | + record MyService(Transformer transformer) { |
| 72 | + |
| 73 | + String transform(String input) throws Exception { |
| 74 | + return "result: " + this.transformer.transform(input).get(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Configuration(proxyBeanMethods = false) |
| 79 | + @EnableAsync |
| 80 | + static class Config { |
| 81 | + |
| 82 | + @Bean |
| 83 | + Transformer transformer() { |
| 84 | + return input -> completedFuture(input.toUpperCase()); |
| 85 | + } |
| 86 | + |
| 87 | + @Bean |
| 88 | + MyService myService(Transformer transformer) { |
| 89 | + return new MyService(transformer); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments