mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish "Restore proxying of @Bean methods in @TestConfiguration"
See gh-18675
This commit is contained in:
parent
7faa606920
commit
fd94608f73
@ -6840,7 +6840,7 @@ The following example shows a `RestDocumentationResultHandler` being defined:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class ResultHandlerConfiguration {
|
||||
|
||||
@Bean
|
||||
|
@ -24,7 +24,7 @@ import org.springframework.restdocs.templates.TemplateFormats;
|
||||
public class AdvancedConfigurationExample {
|
||||
|
||||
// tag::configuration[]
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
public static class CustomizationConfiguration implements RestDocsRestAssuredConfigurationCustomizer {
|
||||
|
||||
@Override
|
||||
|
@ -23,7 +23,7 @@ import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation
|
||||
public class AdvancedConfigurationExample {
|
||||
|
||||
// tag::configuration[]
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
public static class CustomizationConfiguration implements RestDocsWebTestClientConfigurationCustomizer {
|
||||
|
||||
@Override
|
||||
|
@ -49,7 +49,7 @@ class SampleWebClientTests {
|
||||
assertThat(headers.getLocation()).hasHost("other.example.com");
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
|
@ -25,7 +25,7 @@ import org.springframework.boot.test.context.TestConfiguration;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
@EntityScan("some.other.package")
|
||||
public class ExampleTestConfig {
|
||||
|
||||
|
@ -48,7 +48,7 @@ class AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests {
|
||||
assertThat(this.context.getBeanNamesForType(DataSource.class)).isNotEmpty();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ class MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests {
|
||||
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class CustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
|
@ -82,7 +82,7 @@ class RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests
|
||||
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class CustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
|
@ -72,7 +72,7 @@ class WebTestClientRestDocsAutoConfigurationAdvancedConfigurationIntegrationTest
|
||||
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class CustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
|
@ -44,7 +44,7 @@ class WebMvcTestServletFilterRegistrationDisabledIntegrationTests {
|
||||
this.mvc.perform(get("/one")).andExpect(header().string("x-test", (String) null));
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class DisabledRegistrationConfiguration {
|
||||
|
||||
@Bean
|
||||
|
@ -40,7 +40,7 @@ import org.springframework.core.annotation.AliasFor;
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Configuration
|
||||
@TestComponent
|
||||
public @interface TestConfiguration {
|
||||
|
||||
@ -71,8 +71,8 @@ public @interface TestConfiguration {
|
||||
* individually like when declared on non-{@code @Configuration} classes, a.k.a.
|
||||
* "@Bean Lite Mode" (see {@link Bean @Bean's javadoc}). It is therefore behaviorally
|
||||
* equivalent to removing the {@code @Configuration} stereotype.
|
||||
* @since 2.2.1
|
||||
* @return whether to proxy {@code @Bean} methods
|
||||
* @since 2.2.1
|
||||
*/
|
||||
@AliasFor(annotation = Configuration.class)
|
||||
boolean proxyBeanMethods() default true;
|
||||
|
@ -16,39 +16,42 @@
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link TestConfiguration}.
|
||||
* Tests for {@link TestConfiguration @TestConfiguration}.
|
||||
*
|
||||
* @author Dmytro Nosan
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class TestConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
void shouldProxyBeanMethods() {
|
||||
this.contextRunner.withUserConfiguration(ProxyBeanMethodsConfiguration.class)
|
||||
.run((context) -> Assertions.assertThat(context).hasFailed());
|
||||
void proxyBeanMethodsIsEnabledByDefault() {
|
||||
AnnotationAttributes attributes = AnnotatedElementUtils
|
||||
.getMergedAnnotationAttributes(DefaultTestConfiguration.class, Configuration.class);
|
||||
assertThat(attributes.get("proxyBeanMethods")).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotProxyBeanMethods() {
|
||||
this.contextRunner.withUserConfiguration(ProxyBeanMethodsDisableConfiguration.class)
|
||||
.run((context) -> Assertions.assertThat(context).hasNotFailed());
|
||||
void proxyBeanMethodsCanBeDisabled() {
|
||||
AnnotationAttributes attributes = AnnotatedElementUtils
|
||||
.getMergedAnnotationAttributes(NoBeanMethodProxyingTestConfiguration.class, Configuration.class);
|
||||
assertThat(attributes.get("proxyBeanMethods")).isEqualTo(false);
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
final static class ProxyBeanMethodsConfiguration {
|
||||
static class DefaultTestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
final static class ProxyBeanMethodsDisableConfiguration {
|
||||
static class NoBeanMethodProxyingTestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
|
@ -67,7 +67,7 @@ class SpringBootTestContextBootstrapperIntegrationTests {
|
||||
assertThat(this.defaultTestExecutionListenersPostProcessorCalled).isTrue();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class TestConfig {
|
||||
|
||||
@Bean
|
||||
|
Loading…
Reference in New Issue
Block a user