|
| 1 | +/* |
| 2 | + * Copyright 2002-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.integration; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.context.annotation.Import; |
| 25 | +import org.springframework.test.context.bean.override.example.ExampleGenericService; |
| 26 | +import org.springframework.test.context.bean.override.example.ExampleGenericServiceCaller; |
| 27 | +import org.springframework.test.context.bean.override.example.IntegerExampleGenericService; |
| 28 | +import org.springframework.test.context.bean.override.example.StringExampleGenericService; |
| 29 | +import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
| 30 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.mockito.BDDMockito.then; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests that {@link MockitoSpyBean @MockitoSpyBean} on a field with generics can |
| 37 | + * be used to replace an existing bean with matching generics. |
| 38 | + * |
| 39 | + * @author Phillip Webb |
| 40 | + * @author Sam Brannen |
| 41 | + * @since 6.2 |
| 42 | + * @see MockitoBeanWithGenericsOnTestFieldForNewBeanIntegrationTests |
| 43 | + */ |
| 44 | +@SpringJUnitConfig |
| 45 | +class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests { |
| 46 | + |
| 47 | + @MockitoSpyBean |
| 48 | + ExampleGenericService<String> service; |
| 49 | + |
| 50 | + @Autowired |
| 51 | + ExampleGenericServiceCaller caller; |
| 52 | + |
| 53 | + |
| 54 | + @Test |
| 55 | + void testSpying() { |
| 56 | + assertThat(caller.sayGreeting()).isEqualTo("I say Enigma 123"); |
| 57 | + then(service).should().greeting(); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + @Configuration(proxyBeanMethods = false) |
| 62 | + @Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class }) |
| 63 | + static class SpyBeanOnTestFieldForExistingBeanConfig { |
| 64 | + |
| 65 | + @Bean |
| 66 | + ExampleGenericService<String> simpleExampleStringGenericService() { |
| 67 | + // In order to trigger the issue, we need a method signature that returns the |
| 68 | + // generic type instead of the actual implementation class. |
| 69 | + return new StringExampleGenericService("Enigma"); |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments