Skip to content

Commit

Permalink
Remove support for Mockito annotations and MockitoSession
Browse files Browse the repository at this point in the history
This change remove the support for Mockito annotations, `MockitoSession`
and opening/closing of mocks that was inherited from Boot's `@MockBean`
support, as well as the switch to `MockitoSession` made in 1c893e6.

Attempting to take responsability for things Mockito's own JUnit
Jupiter extension does better is not ideal, and we found it leads to
several corner cases which make `SpringExtension` and `MockitoExtension`
incompatible in the current approach.

Instead, this change refocuses our Mockito bean overriding support
exclusively on aspects specific to the Framework. `MockitoExtension`
will thus be usable in conjunction with `SpringExtension` if one needs
to use `@Captor`/`@InitMocks`/`@Mock`/`@Spy` or other Mockito utilities.

See spring-projectsgh-33318
Closes spring-projectsgh-33692
  • Loading branch information
simonbasle committed Oct 16, 2024
1 parent c261ca3 commit 6f79c7e
Show file tree
Hide file tree
Showing 14 changed files with 5 additions and 432 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ creating unnecessary contexts.
====

Each annotation also defines Mockito-specific attributes to fine-tune the mocking details.
During the test class lifecycle, Mockito is set up via the `Mockito#mockitoSession()`
mechanism. Notably, it enables `STRICT_STUBS` mode by default. This can be changed on
individual test classes with the `@MockitoBeanSettings` annotation.

By default, the `@MockitoBean` annotation uses the `REPLACE_OR_CREATE`
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ by default, exactly in the following order:
annotation for "`before`" modes.
* `ApplicationEventsTestExecutionListener`: Provides support for
xref:testing/testcontext-framework/application-events.adoc[`ApplicationEvents`].
* `MockitoTestExecutionListener`: Initializes and closes mocks configured using `@MockitoBean` or `@MockitoSpyBean`.
* `DependencyInjectionTestExecutionListener`: Provides dependency injection for the test
instance.
* `MicrometerObservationRegistryTestExecutionListener`: Provides support for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@
* DirtiesContextBeforeModesTestExecutionListener}</li>
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener
* ApplicationEventsTestExecutionListener}</li>
* <li>{@link org.springframework.test.context.bean.override.mockito.MockitoTestExecutionListener
* MockitoTestExecutionListener}</li>
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener
* DependencyInjectionTestExecutionListener}</li>
* <li>{@link org.springframework.test.context.observation.MicrometerObservationRegistryTestExecutionListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
* @see org.springframework.test.context.web.ServletTestExecutionListener
* @see org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
* @see org.springframework.test.context.event.ApplicationEventsTestExecutionListener
* @see org.springframework.test.context.bean.override.mockito.MockitoTestExecutionListener
* @see org.springframework.test.context.support.DependencyInjectionTestExecutionListener
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
* @see org.springframework.test.context.support.CommonCachesTestExecutionListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@ abstract class AbstractMockitoTestExecutionListener extends AbstractTestExecutio

private static final String SPRING_MOCKITO_PACKAGE = "org.springframework.test.context.bean.override.mockito";

private static final String ORG_MOCKITO_PACKAGE = "org.mockito";

private static final Predicate<MergedAnnotation<?>> isMockitoAnnotation = mergedAnnotation -> {
String packageName = mergedAnnotation.getType().getPackageName();
return (packageName.startsWith(SPRING_MOCKITO_PACKAGE) ||
packageName.startsWith(ORG_MOCKITO_PACKAGE));
return packageName.startsWith(SPRING_MOCKITO_PACKAGE);
};


/**
* Determine if the test class for the supplied {@linkplain TestContext
* test context} uses {@code org.mockito} annotations or any of the annotations
* in this package (such as {@link MockitoBeanSettings @MockitoBeanSettings}).
* test context} uses any of the annotations in this package (such as
* {@link MockitoBean @MockitoBean}).
*/
static boolean hasMockitoAnnotations(TestContext testContext) {
return hasMockitoAnnotations(testContext.getTestClass());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
* @author Phillip Webb
* @author Sam Brannen
* @since 6.2
* @see MockitoTestExecutionListener
* @see MockitoBean @MockitoBean
* @see MockitoSpyBean @MockitoSpyBean
*/
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion spring-test/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ org.springframework.test.context.TestExecutionListener = \
org.springframework.test.context.web.ServletTestExecutionListener,\
org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener,\
org.springframework.test.context.event.ApplicationEventsTestExecutionListener,\
org.springframework.test.context.bean.override.mockito.MockitoTestExecutionListener,\
org.springframework.test.context.support.DependencyInjectionTestExecutionListener,\
org.springframework.test.context.observation.MicrometerObservationRegistryTestExecutionListener,\
org.springframework.test.context.support.DirtiesContextTestExecutionListener,\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.springframework.core.annotation.AnnotationConfigurationException;
import org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener;
import org.springframework.test.context.bean.override.mockito.MockitoResetTestExecutionListener;
import org.springframework.test.context.bean.override.mockito.MockitoTestExecutionListener;
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
Expand Down Expand Up @@ -69,7 +68,6 @@ void defaultListeners() {
List<Class<?>> expected = asList(ServletTestExecutionListener.class,//
DirtiesContextBeforeModesTestExecutionListener.class,//
ApplicationEventsTestExecutionListener.class,//
MockitoTestExecutionListener.class,//
DependencyInjectionTestExecutionListener.class,//
micrometerListenerClass,//
DirtiesContextTestExecutionListener.class,//
Expand All @@ -92,7 +90,6 @@ void defaultListenersMergedWithCustomListenerPrepended() {
ServletTestExecutionListener.class,//
DirtiesContextBeforeModesTestExecutionListener.class,//
ApplicationEventsTestExecutionListener.class,//
MockitoTestExecutionListener.class,//
DependencyInjectionTestExecutionListener.class,//
micrometerListenerClass,//
DirtiesContextTestExecutionListener.class,//
Expand All @@ -114,7 +111,6 @@ void defaultListenersMergedWithCustomListenerAppended() {
List<Class<?>> expected = asList(ServletTestExecutionListener.class,//
DirtiesContextBeforeModesTestExecutionListener.class,//
ApplicationEventsTestExecutionListener.class,//
MockitoTestExecutionListener.class,//
DependencyInjectionTestExecutionListener.class,//
micrometerListenerClass,//
DirtiesContextTestExecutionListener.class,//
Expand All @@ -137,7 +133,6 @@ void defaultListenersMergedWithCustomListenerInserted() {
List<Class<?>> expected = asList(ServletTestExecutionListener.class,//
DirtiesContextBeforeModesTestExecutionListener.class,//
ApplicationEventsTestExecutionListener.class,//
MockitoTestExecutionListener.class,//
DependencyInjectionTestExecutionListener.class,//
BarTestExecutionListener.class,//
micrometerListenerClass,//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
import static org.mockito.Mockito.times;

/**
* Verifies proper handling of the {@link org.mockito.MockitoSession MockitoSession}
* when a {@link MockitoBean @MockitoBean} field is declared in the enclosing class of
* a {@link Nested @Nested} test class.
* Verifies proper reset of mocks when a {@link MockitoBean @MockitoBean} field
* is declared in the enclosing class of a {@link Nested @Nested} test class.
*
* @author Andy Wilkinson
* @author Sam Brannen
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 6f79c7e

Please sign in to comment.