2016-07-04 111 views
8

我剛發現Spring有一個調試模式,它提供了有關自動配置的見解。對於服務器,可以通過將--debug作爲應用程序參數進行啓用。如何在測試中啓用Spring的自動配置報告?

有沒有辦法啓用調試模式也用於測試(用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 

回答

15

--debug設置一個debug財產然後在自動配置交換機的報告。您可以在您的測試中使用例如@TestPropertySource對您的測試類進行相同的測試:

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