如何在测试中启用Spring的自动配置报告?

Phi*_*ßen 8 java spring spring-boot

我刚刚发现Spring有一个调试模式,它可以提供有关自动配置的见解.对于服务器,可以通过--debug作为应用程序参数传递来启用它.

有没有办法为测试启用调试模式(用它执行SpringJUnit4ClassRunner)?


如果自动配置报告正在运行,它应该打印一些输出,如下所示:

=========================
AUTO-CONFIGURATION REPORT
=========================


Positive matches:
-----------------

   ConfigServiceBootstrapConfiguration#configServicePropertySource matched
      - matched (OnPropertyCondition)

   ConfigurationPropertiesRebinderAutoConfiguration matched
      - @ConditionalOnBean (types: org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; SearchStrategy: all) found the following [org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor] (OnBeanCondition)

   ConfigurationPropertiesRebinderAutoConfiguration#configurationPropertiesBeans matched
      - @ConditionalOnMissingBean (types: org.springframework.cloud.context.properties.ConfigurationPropertiesBeans; SearchStrategy: current) found no beans (OnBeanCondition)

   ConfigurationPropertiesRebinderAutoConfiguration#configurationPropertiesRebinder matched
      - @ConditionalOnMissingBean (types: org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; SearchStrategy: current) found no beans (OnBeanCondition)

   EncryptionBootstrapConfiguration matched
      - @ConditionalOnClass classes found: org.springframework.security.crypto.encrypt.TextEncryptor (OnClassCondition)

   PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer matched
      - @ConditionalOnMissingBean (types: org.springframework.context.support.PropertySourcesPlaceholderConfigurer; SearchStrategy: current) found no beans (OnBeanCondition)


Negative matches:
-----------------

   ConfigServiceBootstrapConfiguration.RetryConfiguration did not match
      - required @ConditionalOnClass classes not found: org.springframework.retry.annotation.Retryable,org.aspectj.lang.annotation.Aspect (OnClassCondition)

   DiscoveryClientConfigServiceBootstrapConfiguration did not match
      - @ConditionalOnProperty missing required properties spring.cloud.config.discovery.enabled (OnPropertyCondition)

   EncryptionBootstrapConfiguration.RsaEncryptionConfiguration did not match
      - @ConditionalOnClass classes found: org.springframework.security.rsa.crypto.RsaSecretEncryptor (OnClassCondition)
      - Keystore nor key found in Environment (EncryptionBootstrapConfiguration.KeyCondition)

   EncryptionBootstrapConfiguration.VanillaEncryptionConfiguration did not match
      - required @ConditionalOnMissing classes found: org.springframework.security.rsa.crypto.RsaSecretEncryptor (OnClassCondition)

   EurekaDiscoveryClientConfigServiceBootstrapConfiguration did not match
      - @ConditionalOnClass classes found: org.springframework.cloud.config.client.ConfigServicePropertySourceLocator (OnClassCondition)
      - @ConditionalOnProperty missing required properties spring.cloud.config.discovery.enabled (OnPropertyCondition)


Exclusions:
-----------

    None


Unconditional classes:
----------------------

    None
Run Code Online (Sandbox Code Playgroud)

And*_*son 18

--debug设置一个debug属性,然后打开自动配置报告.您可以在测试中使用,例如,@TestPropertySource在测试类上执行相同的操作:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
@TestPropertySource(properties = "debug=true")
public class YourTests {
    // …
}
Run Code Online (Sandbox Code Playgroud)