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

时间:2016-07-04 16:38:56

标签: 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

1 个答案:

答案 0 :(得分:18)

--debug设置debug属性,然后启用自动配置报告。您可以在测试中使用,例如,在测试类上使用@TestPropertySource

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