Deprecate @MockBean and @SpyBean

- Deprecate Deprecate @MockBean and @SpyBean in favor of Spring
  Framework 6.2's @MockitoBean and @MockitoSpy
- Migrate usages of @MockBean and @SpyBean to @MockitoBean and
  @MockitoSpy

Signed-off-by: Jakob Wanger <jakobwanger@gmail.com>

See gh-39864
This commit is contained in:
Jakob Wanger 2024-03-10 19:42:45 -04:00 committed by Andy Wilkinson
parent acdaa6dc35
commit 24763940a0
38 changed files with 87 additions and 33 deletions

View File

@ -22,7 +22,7 @@ new File(projectDir.parentFile, "gradle.properties").withInputStream {
}
}
versions["jackson"] = "2.15.3"
versions["springFramework"] = "6.0.12"
versions["springFramework"] = "6.2.0-SNAPSHOT"
ext.set("versions", versions)
if (versions.springFramework.contains("-")) {
repositories {

View File

@ -177,6 +177,7 @@ dependencies {
testImplementation("org.springframework.restdocs:spring-restdocs-webtestclient")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("org.yaml:snakeyaml")
testImplementation("org.springframework:spring-test")
testRuntimeOnly("jakarta.management.j2ee:jakarta.management.j2ee-api")
testRuntimeOnly("jakarta.transaction:jakarta.transaction-api")

View File

@ -26,10 +26,10 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.audit.AuditEventRepository;
import org.springframework.boot.actuate.audit.AuditEventsEndpoint;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.bean.override.mockito.MockitoBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
@ -48,7 +48,7 @@ import static org.springframework.restdocs.request.RequestDocumentation.queryPar
*/
class AuditEventsEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
@MockBean
@MockitoBean
private AuditEventRepository repository;
@Test

View File

@ -41,6 +41,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.bean.override.mockito.MockitoBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -56,7 +57,7 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.response
*/
class HttpExchangesEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
@MockBean
@MockitoBean
private HttpExchangeRepository repository;
@Test

View File

@ -38,6 +38,7 @@ import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.bean.override.mockito.MockitoBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -62,7 +63,7 @@ class LoggersEndpointDocumentationTests extends MockMvcEndpointDocumentationTest
.type(JsonFieldType.STRING)
.optional(), fieldWithPath("members").description("Loggers that are part of this group"));
@MockBean
@MockitoBean
private LoggingSystem loggingSystem;
@Autowired

View File

@ -178,7 +178,7 @@ class QuartzEndpointDocumentationTests extends MockMvcEndpointDocumentationTests
.type(JsonFieldType.OBJECT)
.description("Job data map keyed by name, if any.") };
@MockBean
@MockitoBean
private Scheduler scheduler;
@Test

View File

@ -35,6 +35,7 @@ import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.MapSession;
import org.springframework.session.Session;
import org.springframework.test.bean.override.mockito.MockitoBean;
import org.springframework.test.context.TestPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
@ -72,7 +73,7 @@ class SessionsEndpointDocumentationTests extends MockMvcEndpointDocumentationTes
.description("Maximum permitted period of inactivity, in seconds, before the session will expire."),
fieldWithPath("expired").description("Whether the session has expired."));
@MockBean
@MockitoBean
private FindByIndexNameSessionRepository<Session> sessionRepository;
@Test

View File

@ -145,7 +145,7 @@ JUnit 5 enables a test class to be instantiated once and reused for all of the c
This makes it possible to use `@BeforeAll` and `@AfterAll` annotations on non-static methods, which is a good fit for Kotlin.
To mock Kotlin classes, https://mockk.io/[MockK] is recommended.
If you need the `MockK` equivalent of the Mockito specific xref:testing/spring-boot-applications.adoc#testing.spring-boot-applications.mocking-beans[`@MockBean` and `@SpyBean` annotations], you can use https://github.com/Ninja-Squad/springmockk[SpringMockK] which provides similar `@MockkBean` and `@SpykBean` annotations.
If you need the `MockK` equivalent of the Mockito specific xref:testing/spring-boot-applications.adoc#testing.spring-boot-applications.mocking-beans[`@MockitoBean` and `@MockitoSpyBean` annotations], you can use https://github.com/Ninja-Squad/springmockk[SpringMockK] which provides similar `@MockkBean` and `@SpykBean` annotations.

View File

@ -20,8 +20,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import static org.assertj.core.api.Assertions.assertThat;
@ -33,7 +33,7 @@ class MyControllerTests {
@Autowired
private MockMvcTester mvc;
@MockBean
@MockitoBean
private UserVehicleService userVehicleService;
@Test

View File

@ -22,7 +22,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.bean.override.mockito.MockitoBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -33,7 +33,7 @@ class MyHtmlUnitTests {
@Autowired
private WebClient webClient;
@MockBean
@MockitoBean
private UserVehicleService userVehicleService;
@Test

View File

@ -20,8 +20,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.bean.override.mockito.MockitoBean;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.mockito.BDDMockito.given;
@ -32,7 +32,7 @@ class MyControllerTests {
@Autowired
private WebTestClient webClient;
@MockBean
@MockitoBean
private UserVehicleService userVehicleService;
@Test

View File

@ -21,14 +21,14 @@ import org.junit.jupiter.api.Test
import org.mockito.BDDMockito.given
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.MediaType
import org.springframework.test.bean.override.mockito.MockitoBean
import org.springframework.test.web.servlet.assertj.MockMvcTester
@WebMvcTest(UserVehicleController::class)
class MyControllerTests(@Autowired val mvc: MockMvcTester) {
@MockBean
@MockitoBean
lateinit var userVehicleService: UserVehicleService
@Test

View File

@ -23,12 +23,12 @@ import org.junit.jupiter.api.Test
import org.mockito.BDDMockito.given
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.bean.override.mockito.MockitoBean
@WebMvcTest(UserVehicleController::class)
class MyHtmlUnitTests(@Autowired val webClient: WebClient) {
@MockBean
@MockitoBean
lateinit var userVehicleService: UserVehicleService
@Test

View File

@ -20,15 +20,15 @@ import org.junit.jupiter.api.Test
import org.mockito.BDDMockito.given
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.MediaType
import org.springframework.test.bean.override.mockito.MockitoBean
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.reactive.server.expectBody
@WebFluxTest(UserVehicleController::class)
class MyControllerTests(@Autowired val webClient: WebTestClient) {
@MockBean
@MockitoBean
lateinit var userVehicleService: UserVehicleService
@Test

View File

@ -55,6 +55,7 @@ dependencies {
testImplementation("org.spockframework:spock-core")
testImplementation("org.springframework:spring-webmvc")
testImplementation("org.springframework:spring-core-test")
testImplementation("org.springframework:spring-test")
testImplementation("org.testng:testng")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine")

View File

@ -23,7 +23,9 @@ import org.springframework.util.ObjectUtils;
*
* @author Phillip Webb
* @see DefinitionsParser
* @deprecated since 3.4.0. See {@link SpyBean} and {@link MockBean} for more details.
*/
@Deprecated(since = "3.4.0")
abstract class Definition {
private static final int MULTIPLIER = 31;

View File

@ -41,7 +41,10 @@ import org.springframework.util.StringUtils;
*
* @author Phillip Webb
* @author Stephane Nicoll
* @deprecated since 3.4.0. See {@link SpyBean} and {@link MockBean} for more details.
*/
@Deprecated(since = "3.4.0")
class DefinitionsParser {
private final Set<Definition> definitions;

View File

@ -91,11 +91,14 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Phillip Webb
* @since 1.4.0
* @see MockitoPostProcessor
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.mockito.MockitoBean}
*/
@Target({ ElementType.TYPE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(MockBeans.class)
@Deprecated(since = "3.4.0")
public @interface MockBean {
/**

View File

@ -33,10 +33,13 @@ import java.lang.annotation.Target;
*
* @author Phillip Webb
* @since 1.4.0
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.mockito.MockitoBean}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Deprecated(since = "3.4.0")
public @interface MockBeans {
/**

View File

@ -37,7 +37,9 @@ import static org.mockito.Mockito.mock;
* A complete definition that can be used to create a Mockito mock.
*
* @author Phillip Webb
* @deprecated since 3.4.0. See {@link MockBean} for more details.
*/
@Deprecated(since = "3.4.0")
class MockDefinition extends Definition {
private static final int MULTIPLIER = 31;

View File

@ -35,7 +35,11 @@ import org.springframework.util.Assert;
* @author Phillip Webb
* @since 1.4.0
* @see ResetMocksTestExecutionListener
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.mockito.MockReset}
*/
@Deprecated(since = "3.4.0")
public enum MockReset {
/**

View File

@ -24,7 +24,9 @@ import java.util.List;
* Beans created using Mockito.
*
* @author Andy Wilkinson
* @deprecated since 3.4.0. See {@link MockBean} for more details.
*/
@Deprecated(since = "3.4.0")
class MockitoBeans implements Iterable<Object> {
private final List<Object> beans = new ArrayList<>();

View File

@ -28,7 +28,10 @@ import org.springframework.test.context.MergedContextConfiguration;
* A {@link ContextCustomizer} to add Mockito support.
*
* @author Phillip Webb
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.BeanOverrideContextCustomizerFactory}
*/
@Deprecated(since = "3.4.0")
class MockitoContextCustomizer implements ContextCustomizer {
private final Set<Definition> definitions;

View File

@ -27,7 +27,10 @@ import org.springframework.test.context.TestContextAnnotationUtils;
* A {@link ContextCustomizerFactory} to add Mockito support.
*
* @author Phillip Webb
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.BeanOverrideContextCustomizerFactory}
*/
@Deprecated(since = "3.4.0")
class MockitoContextCustomizerFactory implements ContextCustomizerFactory {
@Override

View File

@ -76,7 +76,10 @@ import org.springframework.util.StringUtils;
* @author Stephane Nicoll
* @author Andreas Neiser
* @since 1.4.0
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.BeanOverrideBeanPostProcessor}
*/
@Deprecated(since = "3.4.0")
public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor, BeanClassLoaderAware,
BeanFactoryAware, BeanFactoryPostProcessor, Ordered {

View File

@ -37,7 +37,11 @@ import org.springframework.util.ReflectionUtils;
* @author Moritz Halbritter
* @since 1.4.2
* @see ResetMocksTestExecutionListener
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.mockito.MockitoTestExecutionListener}
*/
@Deprecated(since = "3.4.0")
public class MockitoTestExecutionListener extends AbstractTestExecutionListener {
@Override

View File

@ -34,7 +34,10 @@ import org.springframework.core.annotation.MergedAnnotations;
* @author Phillip Webb
* @author Stephane Nicoll
* @see Definition
* @deprecated since 3.4.0. See {@link SpyBean} and {@link MockBean} for more details.
*/
@Deprecated(since = "3.4.0")
class QualifierDefinition {
private final Field field;

View File

@ -43,7 +43,10 @@ import org.springframework.util.ClassUtils;
* @author Phillip Webb
* @since 1.4.0
* @see MockitoTestExecutionListener
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.mockito.MockitoResetTestExecutionListener}
*/
@Deprecated(since = "3.4.0")
public class ResetMocksTestExecutionListener extends AbstractTestExecutionListener {
private static final boolean MOCKITO_IS_PRESENT = ClassUtils.isPresent("org.mockito.MockSettings",

View File

@ -89,7 +89,10 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Phillip Webb
* @since 1.4.0
* @see MockitoPostProcessor
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.mockito.MockitoSpyBean}
*/
@Deprecated(since = "3.4.0")
@Target({ ElementType.TYPE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented

View File

@ -33,10 +33,13 @@ import java.lang.annotation.Target;
*
* @author Phillip Webb
* @since 1.4.0
* @deprecated since 3.4.0 in favor of
* {@link org.springframework.test.bean.override.mockito.MockitoSpyBean}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Deprecated(since = "3.4.0")
public @interface SpyBeans {
/**

View File

@ -37,7 +37,10 @@ import static org.mockito.Mockito.mock;
* A complete definition that can be used to create a Mockito spy.
*
* @author Phillip Webb
* @deprecated since 3.4.0. See {@link SpyBean} for more details.
*/
@Deprecated(since = "3.4.0")
class SpyDefinition extends Definition {
private static final int MULTIPLIER = 31;

View File

@ -27,6 +27,7 @@ import org.springframework.boot.test.context.nestedtests.InheritedNestedTestConf
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.test.bean.override.mockito.MockitoBean;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.times;
@ -41,7 +42,7 @@ import static org.mockito.Mockito.times;
@Import(ActionPerformer.class)
class InheritedNestedTestConfigurationTests {
@MockBean
@MockitoBean
Action action;
@Autowired

View File

@ -26,9 +26,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.test.bean.override.mockito.MockitoBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -48,7 +48,7 @@ class SampleTestApplicationWebIntegrationTests {
@Autowired
private TestRestTemplate restTemplate;
@MockBean
@MockitoBean
private VehicleDetailsService vehicleDetailsService;
@BeforeEach

View File

@ -24,11 +24,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -48,7 +49,7 @@ class UserVehicleControllerApplicationTests {
@Autowired
private ApplicationContext applicationContext;
@MockBean
@MockitoBean
private UserVehicleService userVehicleService;
@Test

View File

@ -23,7 +23,7 @@ import smoketest.test.service.VehicleDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.bean.override.mockito.MockitoBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -39,7 +39,7 @@ class UserVehicleControllerHtmlUnitTests {
@Autowired
private WebClient webClient;
@MockBean
@MockitoBean
private UserVehicleService userVehicleService;
@Test

View File

@ -24,7 +24,7 @@ import smoketest.test.service.VehicleDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.bean.override.mockito.MockitoBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -40,7 +40,7 @@ class UserVehicleControllerSeleniumTests {
@Autowired
private WebDriver webDriver;
@MockBean
@MockitoBean
private UserVehicleService userVehicleService;
@Test

View File

@ -25,10 +25,10 @@ import smoketest.test.service.VehicleIdentificationNumberNotFoundException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import static org.assertj.core.api.Assertions.assertThat;
@ -51,7 +51,7 @@ class UserVehicleControllerTests {
@Autowired
private ApplicationContext applicationContext;
@MockBean
@MockitoBean
private UserVehicleService userVehicleService;
@Test

View File

@ -29,9 +29,9 @@ import smoketest.webservices.service.HumanResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.webservices.server.WebServiceServerTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.test.bean.override.mockito.MockitoBean;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.ws.test.server.RequestCreators;
import org.springframework.ws.test.server.ResponseMatchers;
@ -48,7 +48,7 @@ import static org.mockito.BDDMockito.then;
@ExtendWith(OutputCaptureExtension.class)
class WebServiceServerTestSampleWsApplicationTests {
@MockBean
@MockitoBean
HumanResourceService service;
@Autowired