Support @Mockito[Spy]Bean & @TestBean w/ @DirtiesContext "before method" modes
Closes gh-33783
This commit is contained in:
@@ -10,6 +10,7 @@ by default, exactly in the following order:
|
||||
annotation for "`before`" modes.
|
||||
* `ApplicationEventsTestExecutionListener`: Provides support for
|
||||
xref:testing/testcontext-framework/application-events.adoc[`ApplicationEvents`].
|
||||
* `BeanOverrideTestExecutionListener`: Provides support for xref:testing/testcontext-framework/bean-overriding.adoc[] .
|
||||
* `DependencyInjectionTestExecutionListener`: Provides dependency injection for the test
|
||||
instance.
|
||||
* `MicrometerObservationRegistryTestExecutionListener`: Provides support for
|
||||
@@ -25,7 +26,6 @@ by default, exactly in the following order:
|
||||
* `EventPublishingTestExecutionListener`: Publishes test execution events to the test's
|
||||
`ApplicationContext` (see xref:testing/testcontext-framework/test-execution-events.adoc[Test Execution Events]).
|
||||
* `MockitoResetTestExecutionListener`: Resets mocks as configured by `@MockitoBean` or `@MockitoSpyBean`.
|
||||
* `BeanOverrideTestExecutionListener`: Provides support for xref:testing/testcontext-framework/bean-overriding.adoc[] .
|
||||
|
||||
[[testcontext-tel-config-registering-tels]]
|
||||
== Registering `TestExecutionListener` Implementations
|
||||
|
||||
@@ -66,6 +66,8 @@ package org.springframework.test.context;
|
||||
* DirtiesContextBeforeModesTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener
|
||||
* ApplicationEventsTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener
|
||||
* BeanOverrideTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener
|
||||
* DependencyInjectionTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.observation.MicrometerObservationRegistryTestExecutionListener
|
||||
@@ -82,8 +84,6 @@ package org.springframework.test.context;
|
||||
* EventPublishingTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.bean.override.mockito.MockitoResetTestExecutionListener
|
||||
* MockitoResetTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener
|
||||
* BeanOverrideTestExecutionListener}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
|
||||
@@ -82,6 +82,7 @@ public @interface TestExecutionListeners {
|
||||
* @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.BeanOverrideTestExecutionListener
|
||||
* @see org.springframework.test.context.support.DependencyInjectionTestExecutionListener
|
||||
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
|
||||
* @see org.springframework.test.context.support.CommonCachesTestExecutionListener
|
||||
@@ -89,7 +90,6 @@ public @interface TestExecutionListeners {
|
||||
* @see org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
|
||||
* @see org.springframework.test.context.event.EventPublishingTestExecutionListener
|
||||
* @see org.springframework.test.context.bean.override.mockito.MockitoResetTestExecutionListener
|
||||
* @see org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener
|
||||
*/
|
||||
@AliasFor("value")
|
||||
Class<? extends TestExecutionListener>[] listeners() default {};
|
||||
|
||||
@@ -37,11 +37,15 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
|
||||
public class BeanOverrideTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
/**
|
||||
* Executes almost last ({@code LOWEST_PRECEDENCE - 50}).
|
||||
* Returns {@code 1950}, which ensures that the {@code BeanOverrideTestExecutionListener}
|
||||
* is ordered after the
|
||||
* {@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
|
||||
* DirtiesContextBeforeModesTestExecutionListener} and just before the
|
||||
* {@link DependencyInjectionTestExecutionListener}.
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return LOWEST_PRECEDENCE - 50;
|
||||
return 1950;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,7 +54,7 @@ import org.springframework.util.ClassUtils;
|
||||
*/
|
||||
public class MockitoResetTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
static final boolean mockitoPresent = ClassUtils.isPresent("org.mockito.Mockito",
|
||||
private static final boolean mockitoPresent = ClassUtils.isPresent("org.mockito.Mockito",
|
||||
MockitoResetTestExecutionListener.class.getClassLoader());
|
||||
|
||||
private static final String SPRING_MOCKITO_PACKAGE = "org.springframework.test.context.bean.override.mockito";
|
||||
@@ -62,8 +62,9 @@ public class MockitoResetTestExecutionListener extends AbstractTestExecutionList
|
||||
private static final Predicate<MergedAnnotation<?>> isSpringMockitoAnnotation = mergedAnnotation ->
|
||||
mergedAnnotation.getType().getPackageName().equals(SPRING_MOCKITO_PACKAGE);
|
||||
|
||||
|
||||
/**
|
||||
* Executes before {@link org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener}.
|
||||
* Returns {@code Ordered.LOWEST_PRECEDENCE - 100}.
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
|
||||
@@ -4,6 +4,7 @@ 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.BeanOverrideTestExecutionListener,\
|
||||
org.springframework.test.context.support.DependencyInjectionTestExecutionListener,\
|
||||
org.springframework.test.context.observation.MicrometerObservationRegistryTestExecutionListener,\
|
||||
org.springframework.test.context.support.DirtiesContextTestExecutionListener,\
|
||||
@@ -11,8 +12,7 @@ org.springframework.test.context.TestExecutionListener = \
|
||||
org.springframework.test.context.transaction.TransactionalTestExecutionListener,\
|
||||
org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener,\
|
||||
org.springframework.test.context.event.EventPublishingTestExecutionListener,\
|
||||
org.springframework.test.context.bean.override.mockito.MockitoResetTestExecutionListener,\
|
||||
org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener
|
||||
org.springframework.test.context.bean.override.mockito.MockitoResetTestExecutionListener
|
||||
|
||||
# Default ContextCustomizerFactory implementations for the Spring TestContext Framework
|
||||
#
|
||||
|
||||
@@ -68,6 +68,7 @@ class TestExecutionListenersTests {
|
||||
List<Class<?>> expected = asList(ServletTestExecutionListener.class,//
|
||||
DirtiesContextBeforeModesTestExecutionListener.class,//
|
||||
ApplicationEventsTestExecutionListener.class,//
|
||||
BeanOverrideTestExecutionListener.class,//
|
||||
DependencyInjectionTestExecutionListener.class,//
|
||||
micrometerListenerClass,//
|
||||
DirtiesContextTestExecutionListener.class,//
|
||||
@@ -75,8 +76,7 @@ class TestExecutionListenersTests {
|
||||
TransactionalTestExecutionListener.class,//
|
||||
SqlScriptsTestExecutionListener.class,//
|
||||
EventPublishingTestExecutionListener.class,//
|
||||
MockitoResetTestExecutionListener.class,//
|
||||
BeanOverrideTestExecutionListener.class
|
||||
MockitoResetTestExecutionListener.class//
|
||||
);
|
||||
assertRegisteredListeners(DefaultListenersTestCase.class, expected);
|
||||
}
|
||||
@@ -90,6 +90,7 @@ class TestExecutionListenersTests {
|
||||
ServletTestExecutionListener.class,//
|
||||
DirtiesContextBeforeModesTestExecutionListener.class,//
|
||||
ApplicationEventsTestExecutionListener.class,//
|
||||
BeanOverrideTestExecutionListener.class,//
|
||||
DependencyInjectionTestExecutionListener.class,//
|
||||
micrometerListenerClass,//
|
||||
DirtiesContextTestExecutionListener.class,//
|
||||
@@ -97,8 +98,7 @@ class TestExecutionListenersTests {
|
||||
TransactionalTestExecutionListener.class,//
|
||||
SqlScriptsTestExecutionListener.class,//
|
||||
EventPublishingTestExecutionListener.class,//
|
||||
MockitoResetTestExecutionListener.class,//
|
||||
BeanOverrideTestExecutionListener.class
|
||||
MockitoResetTestExecutionListener.class//
|
||||
);
|
||||
assertRegisteredListeners(MergedDefaultListenersWithCustomListenerPrependedTestCase.class, expected);
|
||||
}
|
||||
@@ -111,6 +111,7 @@ class TestExecutionListenersTests {
|
||||
List<Class<?>> expected = asList(ServletTestExecutionListener.class,//
|
||||
DirtiesContextBeforeModesTestExecutionListener.class,//
|
||||
ApplicationEventsTestExecutionListener.class,//
|
||||
BeanOverrideTestExecutionListener.class,//
|
||||
DependencyInjectionTestExecutionListener.class,//
|
||||
micrometerListenerClass,//
|
||||
DirtiesContextTestExecutionListener.class,//
|
||||
@@ -119,7 +120,6 @@ class TestExecutionListenersTests {
|
||||
SqlScriptsTestExecutionListener.class,//
|
||||
EventPublishingTestExecutionListener.class,//
|
||||
MockitoResetTestExecutionListener.class,//
|
||||
BeanOverrideTestExecutionListener.class,//
|
||||
BazTestExecutionListener.class
|
||||
);
|
||||
assertRegisteredListeners(MergedDefaultListenersWithCustomListenerAppendedTestCase.class, expected);
|
||||
@@ -133,6 +133,7 @@ class TestExecutionListenersTests {
|
||||
List<Class<?>> expected = asList(ServletTestExecutionListener.class,//
|
||||
DirtiesContextBeforeModesTestExecutionListener.class,//
|
||||
ApplicationEventsTestExecutionListener.class,//
|
||||
BeanOverrideTestExecutionListener.class,//
|
||||
DependencyInjectionTestExecutionListener.class,//
|
||||
BarTestExecutionListener.class,//
|
||||
micrometerListenerClass,//
|
||||
@@ -141,8 +142,7 @@ class TestExecutionListenersTests {
|
||||
TransactionalTestExecutionListener.class,//
|
||||
SqlScriptsTestExecutionListener.class,//
|
||||
EventPublishingTestExecutionListener.class,//
|
||||
MockitoResetTestExecutionListener.class,//
|
||||
BeanOverrideTestExecutionListener.class
|
||||
MockitoResetTestExecutionListener.class//
|
||||
);
|
||||
assertRegisteredListeners(MergedDefaultListenersWithCustomListenerInsertedTestCase.class, expected);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.convention;
|
||||
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.MethodMode;
|
||||
import org.springframework.test.context.bean.override.example.ExampleService;
|
||||
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.test.annotation.DirtiesContext.MethodMode.BEFORE_METHOD;
|
||||
|
||||
/**
|
||||
* Integration tests for using {@link TestBean @TestBean} with
|
||||
* {@link DirtiesContext @DirtiesContext} and {@link MethodMode#BEFORE_METHOD}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 6.2
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
class TestBeanWithDirtiesContextBeforeMethodIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
ExampleServiceCaller caller;
|
||||
|
||||
@TestBean
|
||||
ExampleService service;
|
||||
|
||||
@Autowired
|
||||
ExampleService autowiredService;
|
||||
|
||||
|
||||
static ExampleService service() {
|
||||
return mock();
|
||||
}
|
||||
|
||||
|
||||
@RepeatedTest(2)
|
||||
@DirtiesContext(methodMode = BEFORE_METHOD)
|
||||
void testOverride() {
|
||||
assertThat(service).isSameAs(autowiredService);
|
||||
|
||||
given(service.greeting()).willReturn("Spring");
|
||||
assertThat(caller.sayGreeting()).isEqualTo("I say Spring");
|
||||
}
|
||||
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.example;
|
||||
|
||||
/**
|
||||
* Example service implementation for spy tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 6.2
|
||||
*/
|
||||
public class SimpleExampleService extends RealExampleService {
|
||||
|
||||
public SimpleExampleService() {
|
||||
super("simple");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.junit.jupiter.api.RepeatedTest;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.MethodMode;
|
||||
import org.springframework.test.context.bean.override.example.ExampleService;
|
||||
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.annotation.DirtiesContext.MethodMode.BEFORE_METHOD;
|
||||
|
||||
/**
|
||||
* Integration tests for using {@link MockitoBean @MockitoBean} with
|
||||
* {@link DirtiesContext @DirtiesContext} and {@link MethodMode#BEFORE_METHOD}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Sam Brannen
|
||||
* @since 6.2
|
||||
* @see MockitoSpyBeanWithDirtiesContextBeforeMethodIntegrationTests
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
class MockitoBeanWithDirtiesContextBeforeMethodIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
ExampleServiceCaller caller;
|
||||
|
||||
@MockitoBean
|
||||
ExampleService service;
|
||||
|
||||
@Autowired
|
||||
ExampleService autowiredService;
|
||||
|
||||
|
||||
@RepeatedTest(2)
|
||||
@DirtiesContext(methodMode = BEFORE_METHOD)
|
||||
void testMocking() {
|
||||
assertThat(service).isSameAs(autowiredService);
|
||||
|
||||
given(service.greeting()).willReturn("Spring");
|
||||
assertThat(caller.sayGreeting()).isEqualTo("I say Spring");
|
||||
}
|
||||
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.junit.jupiter.api.RepeatedTest;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.MethodMode;
|
||||
import org.springframework.test.context.bean.override.example.ExampleService;
|
||||
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
|
||||
import org.springframework.test.context.bean.override.example.SimpleExampleService;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.springframework.test.annotation.DirtiesContext.MethodMode.BEFORE_METHOD;
|
||||
|
||||
/**
|
||||
* Integration tests for using {@link MockitoSpyBean @MockitoSpyBean} with
|
||||
* {@link DirtiesContext @DirtiesContext} and {@link MethodMode#BEFORE_METHOD}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Sam Brannen
|
||||
* @since 6.2
|
||||
* @see MockitoBeanWithDirtiesContextBeforeMethodIntegrationTests
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
class MockitoSpyBeanWithDirtiesContextBeforeMethodIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
ExampleServiceCaller caller;
|
||||
|
||||
@MockitoSpyBean
|
||||
SimpleExampleService service;
|
||||
|
||||
@Autowired
|
||||
ExampleService autowiredService;
|
||||
|
||||
|
||||
@RepeatedTest(2)
|
||||
@DirtiesContext(methodMode = BEFORE_METHOD)
|
||||
void testSpying() {
|
||||
assertThat(service).isSameAs(autowiredService);
|
||||
|
||||
assertThat(caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(service).should().greeting();
|
||||
}
|
||||
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(SimpleExampleService.class)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
ExampleServiceCaller serviceCaller(ExampleService service) {
|
||||
return new ExampleServiceCaller(service);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user