3 Star 6 Fork 5

jarchan / spring-test-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
chapter_2_s4_override_auto_configuration.md 3.47 KB
一键复制 编辑 原始数据 按行查看 历史
jarchan 提交于 2017-08-24 16:53 . refactor

Chapter 2: Annotations - @OverrideAutoConfiguration

Chapter 1: 基本用法 - 使用Spring Boot Testing工具里提到:

除了单元测试(不需要初始化ApplicationContext的测试)外,尽量将测试配置和生产配置保持一致。比如如果生产配置里启用了AutoConfiguration,那么测试配置也应该启用。因为只有这样才能够在测试环境下发现生产环境的问题,也避免出现一些因为配置不同导致的奇怪问题。

那么当我们想在测试代码里关闭Auto Configuration如何处理?

  1. 方法1:提供另一套测试配置
  2. 方法2:使用@OverrideAutoConfiguration

方法1虽然能够很好的解决问题,但是比较麻烦。而方法2则能够不改变原有配置、不提供新的配置的情况下,就能够关闭Auto Configuration。

在本章节的例子里,我们自己做了一个Auto Configuration类,AutoConfigurationEnableLogger

@Configuration
public class AutoConfigurationEnableLogger {

  private static final Logger LOGGER = LoggerFactory.getLogger(AutoConfigurationEnableLogger.class);

  public AutoConfigurationEnableLogger() {
    LOGGER.info("Auto Configuration Enabled");
  }

}

并且在META-INF/spring.factories里注册了它:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
me.chanjar.annotation.overrideac.AutoConfigurationEnableLogger

这样一来,只要Spring Boot启动了Auto Configuration就会打印出日志:

2017-08-24 16:44:52.789  INFO 13212 --- [           main] m.c.a.o.AutoConfigurationEnableLogger    : Auto Configuration Enabled

例子1:未关闭Auto Configuration

源代码见BootTest

@SpringBootTest
@SpringBootApplication
public class BootTest extends AbstractTestNGSpringContextTests {

  @Test
  public void testName() throws Exception {

  }
}

查看输出的日志,会发现Auto Configuration已经启用。

例子2:关闭Auto Configuration

然后我们用@OverrideAutoConfiguration关闭了Auto Configuration。

源代码见BootTest

@SpringBootTest
@OverrideAutoConfiguration(enabled = false)
@SpringBootApplication
public class BootTest extends AbstractTestNGSpringContextTests {

  @Test
  public void testName() throws Exception {

  }
}

再查看输出的日志,就会发现Auto Configuration已经关闭。

参考文档

Java
1
https://gitee.com/chanjarster/spring-test-examples.git
git@gitee.com:chanjarster/spring-test-examples.git
chanjarster
spring-test-examples
spring-test-examples
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891