Support @⁠Mockito[Spy]Bean & @⁠TestBean w/ @⁠DirtiesContext "before method" modes

Closes gh-33783
This commit is contained in:
Sam Brannen
2024-10-24 15:05:54 +02:00
parent ba8024d077
commit 3b82733e1f
11 changed files with 279 additions and 17 deletions

View File

@@ -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);
}

View File

@@ -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 {
}
}

View File

@@ -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");
}
}

View File

@@ -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 {
}
}

View File

@@ -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);
}
}
}