diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/AbstractMockitoBeanAndGenericsIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/AbstractMockitoBeanAndGenericsIntegrationTests.java new file mode 100644 index 000000000000..87994ac6a827 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/AbstractMockitoBeanAndGenericsIntegrationTests.java @@ -0,0 +1,78 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.springframework.test.context.bean.override.mockito.integration; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; +import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.Something; +import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.Thing; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +/** + * Abstract base class for tests for {@link MockitoBean @MockitoBean} with generics. + * + * @param type of thing + * @param type of something + * @author Madhura Bhave + * @author Sam Brannen + * @since 6.2 + * @see MockitoBeanAndGenericsIntegrationTests + */ +@SpringJUnitConfig +abstract class AbstractMockitoBeanAndGenericsIntegrationTests, S extends Something> { + + @Autowired + T thing; + + @MockitoBean + S something; + + + static class Something { + String speak() { + return "Hi"; + } + } + + static class SomethingImpl extends Something { + } + + abstract static class Thing { + + @Autowired + private S something; + + S getSomething() { + return this.something; + } + } + + static class ThingImpl extends Thing { + } + + @Configuration(proxyBeanMethods = false) + static class Config { + + @Bean + ThingImpl thing() { + return new ThingImpl(); + } + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndGenericsIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndGenericsIntegrationTests.java new file mode 100644 index 000000000000..316b267ebb7b --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndGenericsIntegrationTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.springframework.test.context.bean.override.mockito.integration; + +import org.junit.jupiter.api.Test; + +import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.SomethingImpl; +import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.ThingImpl; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.when; + +/** + * Concrete implementation of {@link AbstractMockitoBeanAndGenericsIntegrationTests}. + * + * @author Madhura Bhave + * @author Sam Brannen + * @since 6.2 + */ +class MockitoBeanAndGenericsIntegrationTests extends AbstractMockitoBeanAndGenericsIntegrationTests { + + @Test + void mockitoBeanShouldResolveConcreteType() { + assertThat(something).isExactlyInstanceOf(SomethingImpl.class); + + when(something.speak()).thenReturn("Hola"); + assertThat(thing.getSomething().speak()).isEqualTo("Hola"); + } + +}