首页 > 其他分享 >【Springboot】玩转复杂单元测试启动类-只测试数据访问层(JPA+Mybatis) 和服务层 以及Junit和TestNG

【Springboot】玩转复杂单元测试启动类-只测试数据访问层(JPA+Mybatis) 和服务层 以及Junit和TestNG

时间:2024-07-12 18:21:34浏览次数:10  
标签:Springboot 启动 JPA boot 单元测试 public SpringBootTest test class

上一篇文章写了一个最复杂的 SpringBootTest启动类,定制化程序奇高,然而有时候仅测试JPA是不够的。

启动类

需求:

  • 测试Spring Data JPA
  • 测试Mybatis
  • 从容器中获得 ObjectMapper
  • 测试单独的 Service
  • 使用TestNG或者使用Junit
  • 阻止Dubbo、Kafka、ElasticSearch 等中间件启动
  • 使用 application-test.properties 配置
  • 使用 Apollo Config 自动配置

基本结构

@SpringBootTest(classes = {JpaConfig.class},
        webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ActiveProfiles("test")
@EnableAutoConfiguration(exclude = {
        DubboAutoConfiguration.class,
        DubboRelaxedBindingAutoConfiguration.class,
        KafkaAutoConfiguration.class,
        ElasticsearchDataAutoConfiguration.class,
        ElasticsearchRepositoriesAutoConfiguration.class,
        ReactiveElasticsearchRepositoriesAutoConfiguration.class,
        ReactiveElasticsearchRestClientAutoConfiguration.class
})
public class DaoTest {
   //...
}

说明

基本测试注解:

  1. @SpringBootTest 声明了 webEnvironment,以及需要扫描的配置类,而不是整个包路径遍历扫描
  2. @ActiveProfiles 声明使用的Profile,对应配置文件 application-test.properties
  3. @EnableAutoConfiguration

其中 JpaConfig.class 是启动 JPA的配置类,该类主要声明 Datasource,以及使用 configurationProperties,从而将Apollo配置中心、配置文件数据源扫描到上下文中。

Mybatis

启动Mybatis的Mapper接口扫描:

  • 其中 @AutoConfigureMybatis 注解 和 @SpringBootTest 兼容。
  • 其中 @AutoConfigureTestDatabase 注解 声明使用真实的数据库,而不是H2。
  • 其中 @MapperScan声明了 dao所在包位置,防止因为启动类包路径不在 dao上层以及dao的包路径存在特殊的位置,而扫描不到Mapper接口的情况。
@MapperScan("com.slankka.company.dao.mapper")
@AutoConfigureMybatis
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

或者通过如下方式制定,并在 DaoTest 类上 Import

//..
@Import(MappersConfig.class)
//..
public class DaoTest {
 //...
}
@TestConfiguration
@ComponentScan("com.slankka.company.dao.mapstruct")
public class MappersConfig {

}

随意指定需要扫描的 Service

  • 方法一:直接在 @SpringBootTest 上声明 classes
  • 方法二:使用单独的类并@Import

例如

@AutoConfigureJsonTesters
@Import({FrameworkServiceImpl.class})
public class EnableFrameworkServiceTestBundle {
}

说明
*不需要任何方法,空类即可,好处是可以分门别类,同时如果待测试的 ServiceImpl 实现类,还缺少某个Bean,同样可以 @Import。

//..
@SpringBootTest(classes = { JpaConfig.class, EnableFrameworkServiceTestBundle.class })
//..
public class DaoTest {
 //...
}

使用容器中的 ObjectMapper

@AutoConfigureJsonTesters
  • @AutoConfigureJsonTesters 的作用之一,是让 spring-boot-test 初始化JSON有关的自动配置类:例如这个注解会导致 JacksonAutoConfiguration 启动,因此容器中就有了ObjectMapper

原理

@ImportAutoConfiguration
@PropertyMapping("spring.test.jsontesters")
public @interface AutoConfigureJsonTesters {

	/**
	 * If {@link BasicJsonTester}, {@link JacksonTester}, {@link JsonbTester} and
	 * {@link GsonTester} beans should be registered. Defaults to {@code true}.
	 * @return if tester support is enabled
	 */
	boolean enabled() default true;

}

其中 ImportAutoConfiguration 导致下方的类型初个自动装备

# AutoConfigureJsonTesters auto-configuration imports
org.springframework.boot.test.autoconfigure.json.JsonTestersAutoConfiguration
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration

TestNg 还是 Junit

TestNg
TestNg 的启动类直接改成:

@SpringBootTest(classes = {JpaConfig.class},
        webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ActiveProfiles("test")
public class DaoTest extends AbstractTestNGSpringContextTests {
   //...
}

JUnit
默认就是JUnit

参考资料
[1] spring boot appendix test-auto-configuration

标签:Springboot,启动,JPA,boot,单元测试,public,SpringBootTest,test,class
From: https://www.cnblogs.com/slankka/p/18299164

相关文章

  • Springboot按天生成日志文件
    原文链接:https://blog.csdn.net/weixin_47798667/article/details/131846942 1:首先再yml文件上加上配置 logging: config:classpath:logback-spring.xml2:新建一个logback-spring.xml文件 文件内容是如下 <?xmlversion="1.0"encoding="UTF-8"?>......
  • 基于SpringBoot+Vue+数据可视化的药品商场购物系统设计和实现(源码+LW+部署讲解)
    博主介绍:✌全网粉丝50W+,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌技术范围:SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、P......
  • 六、 SpringBoot 配置⽂件 ★ ✔
    六、SpringBoot配置⽂件本节⽬标1.配置⽂件作⽤2.配置⽂件快速⼊⼿3.配置⽂件的格式4.properties配置⽂件说明4.1properties基本语法4.2读取配置⽂件4.3properties缺点分析5.yml配置⽂件说明5.1yml基本语法5.2yml使⽤进阶5.2.1yml配置不同数据类型......
  • day07-SpringBoot
    昨日回顾SSM框架的问题配置文件多。所有的都需要手动配置依赖多。而且会出现版本问题。例如json包就出现版本不兼容问题。启动需要借助tomcat。还要手动集成tomcat插件。学习目标基于SpringBoot框架的程序开发步骤熟练使用SpringBoot配置信息修改服务器配置基于Sprin......
  • 毕业设计-基于Springboot+Vue的招生管理系统的设计与实现(源码+LW+包运行)
    源码获取:https://download.csdn.net/download/u011832806/89456200基于SpringBoot+Vue的招生管理系统开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven系统演示视频:链接:https://pan.baidu.com/s/1GwKRBbuwMiZmnxkvRN9VJg?pwd=sb......
  • 毕业设计-基于Springboot+Vue的致远汽车租赁系统的设计与实现(源码+LW+包运行)
    源码获取:https://download.csdn.net/download/u011832806/89456206基于SpringBoot+Vue的致远汽车租赁系统开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven系统演示视频:链接:https://pan.baidu.com/s/1IHfaizhpMI1q750DBZ1enA?pw......
  • 毕业设计-基于Springboot+Vue的智慧外贸平台的设计与实现(源码+LW+包运行)
    源码获取:https://download.csdn.net/download/u011832806/89456212基于SpringBoot+Vue的智慧外贸平台开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven系统演示视频:链接:https://pan.baidu.com/s/1PnEjpc6IXOgPmYxluTOr2g?pwd=kv......
  • 在springboot 中使用Apache HttpClient 4的详细示例
    在SpringBoot中使用ApacheHttpClient,可以通过配置HttpClient的Bean并使用它来发起HTTP请求。下面是一个详细的示例,展示了如何在SpringBoot应用中集成和使用ApacheHttpClient。步骤1:添加依赖在你的pom.xml文件中添加ApacheHttpClient的依赖:<dependency>......
  • 在springboot 中使用Apache HttpClient 5的详细示例
    ApacheHttpComponentsClient5.x是HttpClient的最新版本,与4.x系列相比,5.x系列进行了许多改进和重构,提供了更现代的API和更好的性能。以下是使用步骤步骤1:添加依赖在你的pom.xml文件中添加ApacheHttpClient5.x的依赖:<dependency><groupId>org.apache.htt......
  • springboot快速整合任务
    springboot整合任务有很多种方法,下面以Quartz跟Task作为整合,快速把握。其中Task是比较常用以及我个人推荐,而且上手比较简单。Task技术整合spring根据定时任务的特征,将定时任务的开发简化到了极致。在springboot项目中使用也是同样的道理。只要设置一个定时任务告诉容器有,然后定......