关于java:如何创建自己的Spring-Boot-Starter并为其编写单元测试

4次阅读

共计 3104 个字符,预计需要花费 8 分钟才能阅读完成。

当咱们想要封装一些自定义性能给他人应用的时候,创立 Spring Boot Starter 的模式是最好的实现形式。如果您还不会构建本人的 Spring Boot Starter 的话,本文将带你一起创立一个本人的 Spring Boot Starter。

疾速入门

  1. 创立一个新的 Maven 我的项目。第三方封装的命名格局是 xxx-spring-boot-starter,例如:didispace-spring-boot-starter
  2. 编辑 pom.xml,增加spring-boot-autoconfigurespring-boot-starter依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
</dependencies>
  1. 创立一个用 @Configuration 正文的配置类,在这里您能够应用 @Bean 来创立应用 @ConditionalOnClass@ConditionalOnMissingBean 等条件正文来管制何时利用配置。
@Configuration
@ConditionalOnClass(MyFeature.class)
@ConditionalOnProperty(prefix = "myfeature", name = "enabled", matchIfMissing = true)
public class MyFeatureAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MyFeature myFeature() {return new MyFeature();
    }
}
  1. src/main/resources/META-INF 目录下创立 spring.factories 文件,并在 org.springframework.boot.autoconfigure.EnableAutoConfiguration 关键字下列出您的主动配置类,比方:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.didispace.myfeature.MyFeatureAutoConfiguration

该配置的作用是让 Spring Boot 利用在引入您自定义 Starter 的时候能够主动这里的配置类。

留神:Spring Boot 2.7 开始,不再举荐应用spring.factories,而是改用/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,文件内容间接放须要主动加载配置类门路即可。这个变更具体可见之前的这篇文章:《Spring Boot 2.7 开始 spring.factories 不举荐应用了》

验证测试

在制作 Spring Boot Starter 的时候,肯定记得应用单元测试来验证和确保自动化配置类在任何条件逻辑在启动器下可能依照正确的预期运行。

创立单元测试

应用 @SpringBootTest 加载残缺的应用程序上下文,并验证启动程序是否正确配置了 Bean 和属性。

@SpringBootTest(classes = TestApplication.class)
public class MyStarterAutoConfigurationTest {@Autowired(required = false)
    private MyService myService;

    @Test
    public void testMyServiceAutoConfigured() {assertNotNull(myService, "MyService should be auto-configured");
    }
}

笼罩不同的配置

如果有不同的配置计划,那么还须要应用 @TestPropertySource@DynamicPropertySource笼罩属性以测试不同配置下的状况。

或者也能够间接简略的通过 @SpringBootTest 中的属性来配置,比方上面这样:

@SpringBootTest(properties = "my.starter.custom-property=customValue")
public class MyStarterPropertiesTest {@Value("${my.starter.custom-property}")
    private String customProperty;

    @Test
    public void testPropertyOverride() {assertEquals("customValue", customProperty, "Custom property should be overridden by @SpringBootTest");
    }
}

笼罩 @Conditional 的不同分支

如果您的启动器蕴含条件配置,比方:@ConditionalOnProperty@ConditionalOnClass等注解,那么就必须编写测试来笼罩所有条件以验证是否已正确。

比方上面这样:

@SpringBootTest(classes = {TestApplication.class, MyConditionalConfiguration.class})
@ConditionalOnProperty(name = "my.starter.enable", havingValue = "true")
public class MyStarterConditionalTest {

    @Autowired
    private ApplicationContext context;

    @Test
    public void conditionalBeanNotLoadedWhenPropertyIsFalse() {
        assertFalse(context.containsBean("conditionalBean"),
            "Conditional bean should not be loaded when'my.starter.enable'is false"
        );
    }
}

为了笼罩不同的条件分支,咱们通常还须要应用 @TestConfiguration 注解来有选择地启用或禁用某些主动配置。

小结

本文介绍了两个 Spring Boot 的进阶内容:

  1. 如何创立 Spring Boot Starter
  2. 如何为 Spring Boot Starter 提供单元测试

把握这项技能能够帮你更好的为 Spring Boot 提供模块划的性能封装。如果您学习过程中如遇艰难?能够退出咱们超高品质的 Spring 技术交换群,参加交换与探讨,更好的学习与提高!更多 Spring Boot 教程能够点击中转!,欢送珍藏与转发反对!

最初再给大家举荐一些无关 Spring Boot Starter 和自动化配置的扩大浏览:

  • Spring Boot Starter 配置 spring.factories 的主动生成神器
  • Spring Boot 自动化配置的利弊及解决之道

欢送关注我的公众号:程序猿 DD。第一工夫理解前沿行业音讯、分享深度技术干货、获取优质学习资源

正文完
 0