当前位置: 首页 > news >正文

如何创建自己的 Spring Boot Starter 并为其编写单元测试

当我们想要封装一些自定义功能给别人使用的时候,创建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@ConditionalOnMissingBeanpublic 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不推荐使用了》:

https://www.didispace.com/article/spring-boot/spring-boot-factories-deprecations.html

验证测试

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

创建单元测试

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

@SpringBootTest(classes = TestApplication.class)
public class MyStarterAutoConfigurationTest {@Autowired(required = false)private MyService myService;@Testpublic 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;@Testpublic 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 {@Autowiredprivate ApplicationContext context;@Testpublic void conditionalBeanNotLoadedWhenPropertyIsFalse() {assertFalse(context.containsBean("conditionalBean"),"Conditional bean should not be loaded when 'my.starter.enable' is false");}
}

为了覆盖不同的条件分支,我们通常还需要使用@TestConfiguration注解来有选择地启用或禁用某些自动配置。

http://www.lryc.cn/news/330471.html

相关文章:

  • C++ :STL中deque的原理
  • AttributeError: ‘Namespace‘ object has no attribute ‘EarlyStopping‘
  • 深度学习pytorch——卷积神经网络(持续更新)
  • 【edge浏览器无法登录某些网站,以及迅雷插件无法生效的解决办法】
  • OpenHarmony无人机MAVSDK开源库适配方案分享
  • 模型训练----parser.add_argument添加配置参数
  • 数字未来:探索 Web3 的革命性潜力
  • 群晖NAS使用Docker部署大语言模型Llama 2结合内网穿透实现公网访问本地GPT聊天服务
  • [选型必备基础信息] 存储器
  • C++——C++11线程库
  • 机器学习 | 线性判别分析(Linear Discriminant Analysis)
  • TypeScript-数组、函数类型
  • Python深度学习034:cuda的环境如何配置
  • 【论文笔记】Text2QR
  • 【ReadPapers】A Survey of Large Language Models
  • 站群CMS系统
  • landsat8数据产品说明
  • Golang 内存管理和垃圾回收底层原理(二)
  • OpenHarmony:全流程讲解如何编写ADC平台驱动以及应用程序
  • 计算机学生求职简历的一些想法
  • 网工内推 | 售前专场,需熟悉云计算技术,上市公司,提成高
  • excel匹配替换脱敏身份证等数据
  • [技术笔记] Flash选型之基础知识芯片分类
  • Jenkins常用插件安装及全局配置
  • C++初学者:如何优雅地写程序
  • 图论- 最小生成树
  • LeetCode刷题记(一):1~30题
  • 芒果YOLOv5改进89:卷积SPConv篇,即插即用,去除特征图中的冗余,FLOPs 和参数急剧下降,提升小目标检测
  • Linux:详解TCP报头类型
  • 【Leetcode】top 100 二分查找