资讯专栏INFORMATION COLUMN

Spring、Spring Boot和TestNG测试指南 - @TestConfiguration

wangtdgoodluck / 1331人阅读

摘要:地址是提供的一种工具,用它我们可以在一般的之外补充测试专门用的或者自定义的配置。实际上是一种,是另一种,在语义上用来指定某个是专门用于测试的。所以我们在测试代码上添加,用或者在同里添加类都是可以的。

Github地址

@TestConfiguration是Spring Boot Test提供的一种工具,用它我们可以在一般的@Configuration之外补充测试专门用的Bean或者自定义的配置。

@TestConfiguration实际上是一种@TestComponent,@TestComponent是另一种@Component,在语义上用来指定某个Bean是专门用于测试的。

需要特别注意,你应该使用一切办法避免在生产代码中自动扫描到@TestComponent。
如果你使用@SpringBootApplication启动测试或者生产代码,@TestComponent会自动被排除掉,如果不是则需要像@SpringBootApplication一样添加TypeExcludeFilter

</>复制代码

  1. //...
  2. @ComponentScan(excludeFilters = {
  3. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  4. // ...})
  5. public @interface SpringBootApplication
例子1:作为内部类

@TestConfiguration和@Configuration不同,它不会阻止@SpringBootTest去查找机制(在Chapter 1: 基本用法 - 使用Spring Boot Testing工具 - 例子4提到过),正如@TestConfiguration的javadoc所说,它只是对既有配置的一个补充。

所以我们在测试代码上添加@SpringBootConfiguration,用@SpringBootTest(classes=...)或者在同package里添加@SpringBootConfiguration类都是可以的。

而且@TestConfiguration作为内部类的时候它是会被@SpringBootTest扫描掉的,这点和@Configuration一样。

测试代码TestConfigurationTest:

</>复制代码

  1. @SpringBootTest
  2. @SpringBootConfiguration
  3. public class TestConfigurationTest extends AbstractTestNGSpringContextTests {
  4. @Autowired
  5. private Foo foo;
  6. @Test
  7. public void testPlusCount() throws Exception {
  8. assertEquals(foo.getName(), "from test config");
  9. }
  10. @TestConfiguration
  11. public class TestConfig {
  12. @Bean
  13. public Foo foo() {
  14. return new Foo("from test config");
  15. }
  16. }
  17. }
例子2:对@Configuration的补充和覆盖

@TestConfiguration能够:

补充额外的Bean

覆盖已存在的Bean

要特别注意第二点,@TestConfiguration能够直接覆盖已存在的Bean,这一点正常的@Configuration是做不到的。

我们先提供了一个正常的@Configuration(Config):

</>复制代码

  1. @Configuration
  2. public class Config {
  3. @Bean
  4. public Foo foo() {
  5. return new Foo("from config");
  6. }
  7. }

又提供了一个@TestConfiguration,在里面覆盖了foo Bean,并且提供了foo2 Bean(TestConfig):

</>复制代码

  1. @TestConfiguration
  2. public class TestConfig {
  3. // 这里不需要@Primary之类的机制,直接就能够覆盖
  4. @Bean
  5. public Foo foo() {
  6. return new Foo("from test config");
  7. }
  8. @Bean
  9. public Foo foo2() {
  10. return new Foo("from test config2");
  11. }
  12. }

测试代码TestConfigurationTest:

</>复制代码

  1. @SpringBootTest(classes = { Config.class, TestConfig.class })
  2. public class TestConfigurationTest extends AbstractTestNGSpringContextTests {
  3. @Qualifier("foo")
  4. @Autowired
  5. private Foo foo;
  6. @Qualifier("foo2")
  7. @Autowired
  8. private Foo foo2;
  9. @Test
  10. public void testPlusCount() throws Exception {
  11. assertEquals(foo.getName(), "from test config");
  12. assertEquals(foo2.getName(), "from test config2");
  13. }
  14. }

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

例子3:避免@TestConfiguration被扫描到

在上面的这个例子里的TestConfig是会被@ComponentScan扫描到的,如果要避免被扫描到,在本文开头已经提到过了。

先来看一下没有做任何过滤的情形,我们先提供了一个@SpringBootConfiguration(IncludeConfig):

</>复制代码

  1. @SpringBootConfiguration
  2. @ComponentScan
  3. public interface IncludeConfig {
  4. }

然后有个测试代码引用了它(TestConfigIncludedTest):

</>复制代码

  1. @SpringBootTest(classes = IncludeConfig.class)
  2. public class TestConfigIncludedTest extends AbstractTestNGSpringContextTests {
  3. @Autowired(required = false)
  4. private TestConfig testConfig;
  5. @Test
  6. public void testPlusCount() throws Exception {
  7. assertNotNull(testConfig);
  8. }
  9. }

从这段代码可以看到TestConfig被加载了。

现在我们使用TypeExcludeFilter来过滤@TestConfiguration(ExcludeConfig1):

</>复制代码

  1. @SpringBootConfiguration
  2. @ComponentScan(excludeFilters = {
  3. @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)
  4. })
  5. public interface ExcludeConfig1 {
  6. }

再来看看结果(TestConfigExclude_1_Test):

</>复制代码

  1. @SpringBootTest(classes = ExcludeConfig1.class)
  2. public class TestConfigExclude_1_Test extends AbstractTestNGSpringContextTests {
  3. @Autowired(required = false)
  4. private TestConfig testConfig;
  5. @Test
  6. public void test() throws Exception {
  7. assertNull(testConfig);
  8. }
  9. }

还可以用@SpringBootApplication来排除TestConfig(ExcludeConfig2):

</>复制代码

  1. @SpringBootApplication
  2. public interface ExcludeConfig2 {
  3. }

看看结果(TestConfigExclude_2_Test):

</>复制代码

  1. @SpringBootTest(classes = ExcludeConfig2.class)
  2. public class TestConfigExclude_2_Test extends AbstractTestNGSpringContextTests {
  3. @Autowired(required = false)
  4. private TestConfig testConfig;
  5. @Test
  6. public void testPlusCount() throws Exception {
  7. assertNull(testConfig);
  8. }
  9. }
参考文档

Spring Framework Testing

Spring Boot Testing

Detecting test configuration

Excluding test configuration

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70309.html

相关文章

  • SpringSpring BootTestNG测试指南 - 测试Spring MVC

    摘要:地址提供了,能够很方便的来测试。同时也提供了更进一步简化了测试需要的配置工作。本章节将分别举例说明在不使用和使用下如何对进行测试。例子测试的关键是使用对象,利用它我们能够在不需启动容器的情况下测试的行为。 Github地址 Spring Testing Framework提供了Spring MVC Test Framework,能够很方便的来测试Controller。同时Spring...

    andong777 评论0 收藏0
  • SpringSpring BootTestNG测试指南 - @OverrideAutoConfi

    摘要:因为只有这样才能够在测试环境下发现生产环境的问题,也避免出现一些因为配置不同导致的奇怪问题。而方法则能够不改变原有配置不提供新的配置的情况下,就能够关闭。 Github地址 在Chapter 1: 基本用法 - 使用Spring Boot Testing工具里提到: 除了单元测试(不需要初始化ApplicationContext的测试)外,尽量将测试配置和生产配置保持一致。比如如果生产...

    elisa.yang 评论0 收藏0
  • SpringSpring BootTestNG测试指南 - @JsonTest

    摘要:地址是提供的方便测试序列化反序列化的测试工具,在的文档中有一些介绍。例子简单例子源代码见使用通包下的文件测试结果是否正确或者使用基于的校验例子测试可以用来测试。这个例子里使用了自定义的测试代码例子使用事实上也可以配合一起使用。 Github地址 @JsonTest是Spring Boot提供的方便测试JSON序列化反序列化的测试工具,在Spring Boot的文档中有一些介绍。 需要注...

    Hegel_Gu 评论0 收藏0
  • SpringSpring BootTestNG测试指南 - 使用Spring Testing工具

    摘要:源代码见需要注意的是,如果是专供某个测试类使用的话,把它放到外部并不是一个好主意,因为它有可能会被扫描到,从而产生一些奇怪的问题。 Github地址 既然我们现在开发的是一个Spring项目,那么肯定会用到Spring Framework的各种特性,这些特性实在是太好用了,它能够大大提高我们的开发效率。那么自然而然,你会想在测试代码里也能够利用Spring Framework提供的特...

    Maxiye 评论0 收藏0
  • SpringSpring BootTestNG测试指南 - 共享测试配置

    摘要:地址在使用工具中提到在测试代码之间尽量做到配置共用。本章将列举几种共享测试配置的方法我们可以将测试配置放在一个里,然后在测试或中引用它。也可以利用的及自定义机制,提供自己的用在测试配置上。 Github地址 在使用Spring Boot Testing工具中提到: 在测试代码之间尽量做到配置共用。...能够有效利用Spring TestContext Framework的缓存机制,Ap...

    CHENGKANG 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<