Skip to content

Commit d32d69c

Browse files
committed
Merge pull request #39864 from wanger26
* gh-39864: Polish "Deprecate @MockBean and @SpyBean" Deprecate @MockBean and @SpyBean Closes gh-39864
2 parents acdaa6d + cddf92e commit d32d69c

File tree

123 files changed

+349
-442
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+349
-442
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ dependencies {
171171
testImplementation("org.skyscreamer:jsonassert")
172172
testImplementation("org.springframework:spring-core-test")
173173
testImplementation("org.springframework:spring-orm")
174+
testImplementation("org.springframework:spring-test")
174175
testImplementation("org.springframework.data:spring-data-rest-webmvc")
175176
testImplementation("org.springframework.integration:spring-integration-jmx")
176177
testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc")

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/AuditEventsEndpointDocumentationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import org.springframework.boot.actuate.audit.AuditEvent;
2727
import org.springframework.boot.actuate.audit.AuditEventRepository;
2828
import org.springframework.boot.actuate.audit.AuditEventsEndpoint;
29-
import org.springframework.boot.test.mock.mockito.MockBean;
3029
import org.springframework.context.annotation.Bean;
3130
import org.springframework.context.annotation.Configuration;
3231
import org.springframework.context.annotation.Import;
32+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
3333

3434
import static org.assertj.core.api.Assertions.assertThat;
3535
import static org.mockito.ArgumentMatchers.any;
@@ -48,7 +48,7 @@
4848
*/
4949
class AuditEventsEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
5050

51-
@MockBean
51+
@MockitoBean
5252
private AuditEventRepository repository;
5353

5454
@Test

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/HttpExchangesEndpointDocumentationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
import org.springframework.boot.actuate.web.exchanges.Include;
3636
import org.springframework.boot.actuate.web.exchanges.RecordableHttpRequest;
3737
import org.springframework.boot.actuate.web.exchanges.RecordableHttpResponse;
38-
import org.springframework.boot.test.mock.mockito.MockBean;
3938
import org.springframework.context.annotation.Bean;
4039
import org.springframework.context.annotation.Configuration;
4140
import org.springframework.context.annotation.Import;
4241
import org.springframework.http.HttpHeaders;
4342
import org.springframework.restdocs.payload.JsonFieldType;
43+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
4444

4545
import static org.assertj.core.api.Assertions.assertThat;
4646
import static org.mockito.BDDMockito.given;
@@ -56,7 +56,7 @@
5656
*/
5757
class HttpExchangesEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
5858

59-
@MockBean
59+
@MockitoBean
6060
private HttpExchangeRepository repository;
6161

6262
@Test

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/LoggersEndpointDocumentationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.boot.logging.LoggerConfiguration;
3030
import org.springframework.boot.logging.LoggerGroups;
3131
import org.springframework.boot.logging.LoggingSystem;
32-
import org.springframework.boot.test.mock.mockito.MockBean;
3332
import org.springframework.context.annotation.Bean;
3433
import org.springframework.context.annotation.Configuration;
3534
import org.springframework.context.annotation.Import;
@@ -38,6 +37,7 @@
3837
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
3938
import org.springframework.restdocs.payload.FieldDescriptor;
4039
import org.springframework.restdocs.payload.JsonFieldType;
40+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
4141

4242
import static org.assertj.core.api.Assertions.assertThat;
4343
import static org.mockito.BDDMockito.given;
@@ -62,7 +62,7 @@ class LoggersEndpointDocumentationTests extends MockMvcEndpointDocumentationTest
6262
.type(JsonFieldType.STRING)
6363
.optional(), fieldWithPath("members").description("Loggers that are part of this group"));
6464

65-
@MockBean
65+
@MockitoBean
6666
private LoggingSystem loggingSystem;
6767

6868
@Autowired

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/QuartzEndpointDocumentationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
import org.springframework.boot.actuate.endpoint.Show;
5555
import org.springframework.boot.actuate.quartz.QuartzEndpoint;
5656
import org.springframework.boot.actuate.quartz.QuartzEndpointWebExtension;
57-
import org.springframework.boot.test.mock.mockito.MockBean;
5857
import org.springframework.context.annotation.Bean;
5958
import org.springframework.context.annotation.Configuration;
6059
import org.springframework.context.annotation.Import;
6160
import org.springframework.restdocs.payload.FieldDescriptor;
6261
import org.springframework.restdocs.payload.JsonFieldType;
6362
import org.springframework.scheduling.quartz.DelegatingJob;
63+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
6464
import org.springframework.util.LinkedMultiValueMap;
6565
import org.springframework.util.MultiValueMap;
6666

@@ -178,7 +178,7 @@ class QuartzEndpointDocumentationTests extends MockMvcEndpointDocumentationTests
178178
.type(JsonFieldType.OBJECT)
179179
.description("Job data map keyed by name, if any.") };
180180

181-
@MockBean
181+
@MockitoBean
182182
private Scheduler scheduler;
183183

184184
@Test

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/SessionsEndpointDocumentationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import org.springframework.boot.actuate.context.ShutdownEndpoint;
2828
import org.springframework.boot.actuate.session.SessionsEndpoint;
29-
import org.springframework.boot.test.mock.mockito.MockBean;
3029
import org.springframework.context.annotation.Bean;
3130
import org.springframework.context.annotation.Configuration;
3231
import org.springframework.context.annotation.Import;
@@ -36,6 +35,7 @@
3635
import org.springframework.session.MapSession;
3736
import org.springframework.session.Session;
3837
import org.springframework.test.context.TestPropertySource;
38+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
3939

4040
import static org.assertj.core.api.Assertions.assertThat;
4141
import static org.mockito.BDDMockito.given;
@@ -72,7 +72,7 @@ class SessionsEndpointDocumentationTests extends MockMvcEndpointDocumentationTes
7272
.description("Maximum permitted period of inactivity, in seconds, before the session will expire."),
7373
fieldWithPath("expired").description("Whether the session has expired."));
7474

75-
@MockBean
75+
@MockitoBean
7676
private FindByIndexNameSessionRepository<Session> sessionRepository;
7777

7878
@Test

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/kotlin.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ JUnit 5 enables a test class to be instantiated once and reused for all of the c
145145
This makes it possible to use `@BeforeAll` and `@AfterAll` annotations on non-static methods, which is a good fit for Kotlin.
146146

147147
To mock Kotlin classes, https://mockk.io/[MockK] is recommended.
148-
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.
148+
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.
149149

150150

151151

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/spring-boot-applications.adoc

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -247,38 +247,9 @@ When running tests, it is sometimes necessary to mock certain components within
247247
For example, you may have a facade over some remote service that is unavailable during development.
248248
Mocking can also be useful when you want to simulate failures that might be hard to trigger in a real environment.
249249

250-
Spring Boot includes a `@MockBean` annotation that can be used to define a Mockito mock for a bean inside your `ApplicationContext`.
251-
You can use the annotation to add new beans or replace a single existing bean definition.
252-
The annotation can be used directly on test classes, on fields within your test, or on `@Configuration` classes and fields.
253-
When used on a field, the instance of the created mock is also injected.
254-
Mock beans are automatically reset after each test method.
255-
256-
[NOTE]
257-
====
258-
If your test uses one of Spring Boot's test annotations (such as `@SpringBootTest`), this feature is automatically enabled.
259-
To use this feature with a different arrangement, listeners must be explicitly added, as shown in the following example:
260-
261-
include-code::listener/MyTests[]
262-
====
263-
264-
The following example replaces an existing `RemoteService` bean with a mock implementation:
265-
266-
include-code::bean/MyTests[]
267-
268-
NOTE: `@MockBean` cannot be used to mock the behavior of a bean that is exercised during application context refresh.
269-
By the time the test is executed, the application context refresh has completed and it is too late to configure the mocked behavior.
270-
We recommend using a `@Bean` method to create and configure the mock in this situation.
271-
272-
Additionally, you can use `@SpyBean` to wrap any existing bean with a Mockito `spy`.
273-
See the xref:api:java/org/springframework/boot/test/mock/mockito/SpyBean.html[`SpyBean`] API documentation for full details.
274-
275-
NOTE: While Spring's test framework caches application contexts between tests and reuses a context for tests sharing the same configuration, the use of `@MockBean` or `@SpyBean` influences the cache key, which will most likely increase the number of contexts.
276-
277-
TIP: If you are using `@SpyBean` to spy on a bean with `@Cacheable` methods that refer to parameters by name, your application must be compiled with `-parameters`.
278-
This ensures that the parameter names are available to the caching infrastructure once the bean has been spied upon.
279-
280-
TIP: When you are using `@SpyBean` to spy on a bean that is proxied by Spring, you may need to remove Spring's proxy in some situations, for example when setting expectations using `given` or `when`.
281-
Use `AopTestUtils.getTargetObject(yourProxiedSpy)` to do so.
250+
Spring Framework includes a `@MockitoBean` annotation that can be used to define a Mockito mock for a bean inside your `ApplicationContext`.
251+
Additionally, `@MockitoSpyBean` can be used to define a Mockito spy.
252+
Learn more about these features in the {url-spring-framework-docs}/testing/annotations/integration-spring/annotation-mockitobean.html[Spring Framework documentation].
282253

283254

284255

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/mockingbeans/bean/MyTests.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/mockingbeans/bean/RemoteService.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/mockingbeans/bean/Reverser.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/mockingbeans/listener/MyConfig.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/mockingbeans/listener/MyTests.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/springmvctests/MyControllerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
23-
import org.springframework.boot.test.mock.mockito.MockBean;
2423
import org.springframework.http.MediaType;
24+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
2525
import org.springframework.test.web.servlet.assertj.MockMvcTester;
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,7 +33,7 @@ class MyControllerTests {
3333
@Autowired
3434
private MockMvcTester mvc;
3535

36-
@MockBean
36+
@MockitoBean
3737
private UserVehicleService userVehicleService;
3838

3939
@Test

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/springmvctests/MyHtmlUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
25-
import org.springframework.boot.test.mock.mockito.MockBean;
25+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
2828
import static org.mockito.BDDMockito.given;
@@ -33,7 +33,7 @@ class MyHtmlUnitTests {
3333
@Autowired
3434
private WebClient webClient;
3535

36-
@MockBean
36+
@MockitoBean
3737
private UserVehicleService userVehicleService;
3838

3939
@Test

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/springwebfluxtests/MyControllerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
23-
import org.springframework.boot.test.mock.mockito.MockBean;
2423
import org.springframework.http.MediaType;
24+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
2525
import org.springframework.test.web.reactive.server.WebTestClient;
2626

2727
import static org.mockito.BDDMockito.given;
@@ -32,7 +32,7 @@ class MyControllerTests {
3232
@Autowired
3333
private WebTestClient webClient;
3434

35-
@MockBean
35+
@MockitoBean
3636
private UserVehicleService userVehicleService;
3737

3838
@Test

0 commit comments

Comments
 (0)