1. 概覽
眾所周知,自動配置是Spring Boot的關鍵功能之一, 但測試自動配置可能會很棘手。
在以下部分中,我們將展示ApplicationContextRunner如何簡化自動配置測試。
2. 測試自動化配置方案
ApplicationContextRunner是一個實用程序類,它運行ApplicationContext並提供AssertJ樣式斷言。 最好用作測試類中的字段以便共享配置,然後我們在每個測試中進行自定義:
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
讓我們通過測試一些案例來展示它的魔力。
2.1. 測試Class Condition
在本節中,我們將測試一些使用@ConditionalOnClass和@ConditionalOnMissingClass 註解的自動配置類:
@Configuration
@ConditionalOnClass(ConditionalOnClassIntegrationTest.class)
protected static class ConditionalOnClassConfiguration {
@Bean
public String created() { return "This is created when ConditionalOnClassIntegrationTest is present on the classpath"; } } @Configuration @ConditionalOnMissingClass("com.baeldung.autoconfiguration.ConditionalOnClassIntegrationTest") protected static class ConditionalOnMissingClassConfiguration { @Bean public String missed() { return "This is missed when ConditionalOnClassIntegrationTest is present on the classpath"; } }
我們想測試自動配置是否正確實例化或跳過created和missing beans給定的預期條件。
-
ApplicationContextRunner為我們提供了withUserConfiguration方法,我們可以根據需要提供自動配置,以便為每個測試自定義ApplicationContext。
-
run 方法將 ContextConsumer 作為將斷言應用於上下文的參數。 測試退出時,ApplicationContext將自動關閉:
@Test
public void whenDependentClassIsPresent_thenBeanCreated() { this.contextRunner.withUserConfiguration(ConditionalOnClassConfiguration.class) .run(context -> { assertThat(context).hasBean("created"); assertThat(context.getBean("created")) .isEqualTo("This is created when ConditionalOnClassIntegrationTest is present on the classpath"); }); } @Test public void whenDependentClassIsPresent_thenBeanMissing() { this.contextRunner.withUserConfiguration(ConditionalOnMissingClassConfiguration.class) .run(context -> { assertThat(context).doesNotHaveBean("missed"); }); }
通過前面的示例,我們發現測試classpath上存在某個類的場景的簡單性。但是,當類不在classpath上時,我們如何測試相反的情況呢
這就是FilteredClassLoader發揮作用的地方。它用於在運行時過濾classpath上指定的類:
@Test
public void whenDependentClassIsNotPresent_thenBeanMissing() { this.contextRunner.withUserConfiguration(ConditionalOnClassConfiguration.class) .withClassLoader(new FilteredClassLoader(ConditionalOnClassIntegrationTest.class)) .run((context) -> { assertThat(context).doesNotHaveBean("created"); assertThat(context).doesNotHaveBean(ConditionalOnClassIntegrationTest.class); }); } @Test public void whenDependentClassIsNotPresent_thenBeanCreated() { this.contextRunner.withUserConfiguration(ConditionalOnMissingClassConfiguration.class) .withClassLoader(new FilteredClassLoader(ConditionalOnClassIntegrationTest.class)) .run((context) -> { assertThat(context).hasBean("missed"); assertThat(context).getBean("missed") .isEqualTo("This is missed when ConditionalOnClassIntegrationTest is present on the classpath"); assertThat(context).doesNotHaveBean(ConditionalOnClassIntegrationTest.class); }); }
2.2. 測試 Bean Condition
我們剛剛測試了 @ConditionalOnClass 和 @ConditionalOnMissingClass 註解, 現在 讓我們看看使用@ConditionalOnBean和@ConditionalOnMissingBean註釋時的情況。
首先, 我們同樣需要 一些自動配置的類:
@Configuration
protected static class BasicConfiguration {
@Bean
public String created() { return "This is always created"; } } @Configuration @ConditionalOnBean(name = "created") protected static class ConditionalOnBeanConfiguration { @Bean public String createOnBean() { return "This is created when bean (name=created) is present"; } } @Configuration @ConditionalOnMissingBean(name = "created") protected static class ConditionalOnMissingBeanConfiguration { @Bean public String createOnMissingBean() { return "This is created when bean (name=created) is missing"; } }
然後,我們將像上一節一樣調用withUserConfiguration方法,然後發送我們的自定義配置類來測試自動配置是否在不同的條件下恰當地實例化bean或跳過createOnBean或createOnMissingBean :
@Test
public void whenDependentBeanIsPresent_thenConditionalBeanCreated() { this.contextRunner.withUserConfiguration(BasicConfiguration.class, ConditionalOnBeanConfiguration.class) // ommitted for brevity } @Test public void whenDependentBeanIsNotPresent_thenConditionalMissingBeanCreated() { this.contextRunner.withUserConfiguration(ConditionalOnMissingBeanConfiguration.class) // ommitted for brevity }
2.3. 測試 Property Condition
在本節中,我們測試使用 @ConditionalOnPropertyannotations的自動配置類。
首先,我們需要這個測試的屬性:
com.baeldung.service=custom
然後,我們編寫嵌套的自動配置類,根據前面的屬性創建bean:
@Configuration
@TestPropertySource("classpath:ConditionalOnPropertyTest.properties") protected static class SimpleServiceConfiguration { @Bean @ConditionalOnProperty(name = "com.baeldung.service", havingValue = "default") @ConditionalOnMissingBean public DefaultService defaultService() { return new DefaultService(); } @Bean @ConditionalOnProperty(name = "com.baeldung.service", havingValue = "custom") @ConditionalOnMissingBean public CustomService customService() { return new CustomService(); } }
現在,我們調用withPropertyValues方法來覆蓋每個測試中的屬性值:
@Test
public void whenGivenCustomPropertyValue_thenCustomServiceCreated() { this.contextRunner.withPropertyValues("com.baeldung.service=custom") .withUserConfiguration(SimpleServiceConfiguration.class) .run(context -> { assertThat(context).hasBean("customService"); SimpleService simpleService = context.getBean(CustomService.class); assertThat(simpleService.serve()).isEqualTo("Custom Service"); assertThat(context).doesNotHaveBean("defaultService"); }); } @Test public void whenGivenDefaultPropertyValue_thenDefaultServiceCreated() { this.contextRunner.withPropertyValues("com.baeldung.service=default") .withUserConfiguration(SimpleServiceConfiguration.class) .run(context -> { assertThat(context).hasBean("defaultService"); SimpleService simpleService = context.getBean(DefaultService.class); assertThat(simpleService.serve()).isEqualTo("Default Service"); assertThat(context).doesNotHaveBean("customService"); }); }
3. 結論
總結一下, 這篇教程主要展示 如何使用ApplicationContextRunner運行帶有自定義的ApplicationContext並應用斷言.
我們在這裏介紹了最常用的場景,而不是列出如何自定義ApplicationContext 。
在此期間,請記住ApplicationConetxtRunner適用於非Web應用程序,因此請考慮WebApplicationContextRunner用於基於servlet的Web應用程序,ReactiveWebApplicationContextRunner用於響應式Web應用程序。
本文源代碼,請訪問GitHub。
原文:www.baeldung.com/spring-boot…
作者:baeldung
譯者:Leesen
【精選推薦文章】
智慧手機時代的來臨,RWD網頁設計已成為網頁設計推薦首選
想知道網站建置、網站改版該如何進行嗎?將由專業工程師為您規劃客製化網頁設計及後台網頁設計
廣告預算用在刀口上,網站設計公司幫您達到更多曝光效益