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

淄博 网站建设台州地区网站建设

淄博 网站建设,台州地区网站建设,省技能大赛网站开发方案,企业建站个人建站源码Spring Boot测试框架基础 Spring Boot通过增强Spring测试框架的能力,为开发者提供了一系列简化测试流程的新注解和特性。该框架建立在成熟的Spring测试基础之上,通过自动化配置和专用注解显著提升了测试效率。 核心依赖配置 要使用Spring Boot的全部测试功能,只需在项目中…

Spring Boot测试框架基础

Spring Boot通过增强Spring测试框架的能力,为开发者提供了一系列简化测试流程的新注解和特性。该框架建立在成熟的Spring测试基础之上,通过自动化配置和专用注解显著提升了测试效率。

核心依赖配置

要使用Spring Boot的全部测试功能,只需在项目中添加spring-boot-starter-test依赖(scope为test)。若通过Spring Initializr创建项目,该依赖已默认包含。该starter提供了完整的测试工具链:

org.springframework.bootspring-boot-starter-testtest

主要包含以下测试框架:

  • JUnit 5(默认测试引擎)
  • AssertJ(流式断言库)
  • Hamcrest(匹配器库)
  • Mockito(模拟框架)
  • JSONassert(JSON断言工具)
  • JsonPath(JSON路径查询)
  • Spring Test与Spring Boot Test工具集

核心测试注解

@SpringBootTest注解

作为Spring Boot测试的核心注解,@SpringBootTest显著简化了传统Spring测试中需要多重注解的复杂配置。其核心功能包括:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,properties = {"app.timeout=5000"},args = "--app.name=TestApp",classes = {TestConfig.class}
)
public class IntegrationTest {// 测试代码
}

关键参数说明:

  • webEnvironment:配置Web测试环境
    • MOCK(默认):模拟Servlet环境
    • RANDOM_PORT:随机端口启动真实容器
    • DEFINED_PORT:指定端口启动
    • NONE:非Web环境
  • properties:注入测试属性
  • args:模拟命令行参数
  • classes:显式指定配置类

Mock环境测试

默认情况下,@SpringBootTest使用Mock环境测试Web端点,无需启动实际服务器。结合@AutoConfigureMockMvc可自动配置MockMvc:

@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest {@Autowiredprivate MockMvc mockMvc;@Testvoid shouldReturnUser() throws Exception {mockMvc.perform(get("/users/1")).andExpect(status().isOk()).andExpect(jsonPath("$.name").value("TestUser"));}
}

测试切片技术

Spring Boot提供细粒度的"测试切片"机制,允许隔离测试特定层次:

@WebMvcTest

专注控制器层测试,自动配置MockMvc:

@WebMvcTest(UserController.class)
public class UserControllerSliceTest {@Autowiredprivate MockMvc mockMvc;@MockBeanprivate UserService userService;@Testvoid shouldGetUser() throws Exception {when(userService.findById(any())).thenReturn(new User("test"));mockMvc.perform(get("/users/1")).andExpect(content().json("{'name':'test'}"));}
}
@DataJpaTest

专注JPA持久层测试,自动配置数据库和事务:

@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
public class UserRepositoryTest {@Autowiredprivate TestEntityManager entityManager;@Autowired private UserRepository repository;@Testvoid shouldFindByEmail() {entityManager.persist(new User("test@email.com"));User user = repository.findByEmail("test@email.com");assertThat(user).isNotNull();}
}
@JsonTest

专注JSON序列化/反序列化测试:

@JsonTest
public class UserJsonTest {@Autowiredprivate JacksonTester jsonTester;@Testvoid shouldSerialize() throws Exception {User user = new User("test@email.com");JsonContent json = jsonTester.write(user);assertThat(json).extractingJsonPathValue("$.email")).isEqualTo("test@email.com");}
}

测试容器支持

Spring Boot 3.1+原生支持Testcontainers,通过以下注解实现集成测试:

@SpringBootTest
@Testcontainers
public class UserIntegrationTest {@Container@ServiceConnectionstatic PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:15");@Testvoid shouldConnectToDatabase() {// 测试代码}
}

关键注解:

  • @Testcontainers:管理容器生命周期
  • @Container:标记测试容器实例
  • @ServiceConnection:自动配置服务连接

通过这种模块化的测试方法,开发者可以针对不同层级编写精确的测试用例,既保证测试覆盖率,又维持测试执行效率。各测试切片自动配置所需的Spring组件,同时排除无关的自动配置,实现了测试的精准性和高效性。

Mock环境与Web应用测试

Mock环境配置

Spring Boot的@SpringBootTest注解默认采用Mock环境进行Web应用测试,这种模式不会启动实际服务器,而是通过模拟Servlet环境来测试Web端点。要启用此功能,需要结合使用@AutoConfigureMockMvc注解和MockMvc类:

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("mockMvc")
public class UserMockMvcTests {@AutowiredMockMvc mockMvc;@Testvoid createUserTests() throws Exception 

文章转载自:

http://yiZ7aNGR.Lsqnw.cn
http://n3XsxEgc.Lsqnw.cn
http://sAGsvaVn.Lsqnw.cn
http://jDJqeMI5.Lsqnw.cn
http://6qopMsFr.Lsqnw.cn
http://JYeUsb8O.Lsqnw.cn
http://cgUaJ3yj.Lsqnw.cn
http://3MEfPcGI.Lsqnw.cn
http://NyVfeDLn.Lsqnw.cn
http://YN04xI1M.Lsqnw.cn
http://s2oh0raM.Lsqnw.cn
http://11g5xHHy.Lsqnw.cn
http://h8LZqiII.Lsqnw.cn
http://dpvDFRj9.Lsqnw.cn
http://x0BKLoUj.Lsqnw.cn
http://b64SFtiD.Lsqnw.cn
http://DNBhgwB2.Lsqnw.cn
http://r5eqZqq4.Lsqnw.cn
http://J2u9kPKn.Lsqnw.cn
http://BcWJd3VQ.Lsqnw.cn
http://RpTVeU4Y.Lsqnw.cn
http://Ix0r3NQX.Lsqnw.cn
http://rYJx1kUN.Lsqnw.cn
http://7y115PlH.Lsqnw.cn
http://e4jV48fn.Lsqnw.cn
http://59cJeaU7.Lsqnw.cn
http://A6wvclbx.Lsqnw.cn
http://MStbX9w1.Lsqnw.cn
http://qbafM4ll.Lsqnw.cn
http://mesOY0RF.Lsqnw.cn
http://www.dtcms.com/wzjs/613453.html

相关文章:

  • 生成二维码的网站鞍山做网站的公司
  • 做视频解析网站违法不wordpress悬浮工具
  • 成都网站制作成都网站制作电商 做图 网站有哪些
  • 行业门户网站建设方案模板网站和定
  • 网站开发外包 价格网站 目录访问
  • 八旬老太做直播 什么网站靖江网站建设价格
  • 街头小吃加盟网站建设济宁亿峰科技做网站一年多少费用
  • 益阳网站建设不支持下载的视频怎么保存下来
  • 成都网站快速优化排名wordpress 二开北京
  • 响应式网站介绍怎样做外贸
  • 系统网站主题有哪些问题互联网保险销售行为可回溯
  • 国外的设计网站推荐手机软件开发培训班
  • 建设 网站工作汇报google关键词推广
  • 摄影网站制作步骤html工信网查询查询系统
  • 网站推广策略成功的案例网页设计图片平移
  • 基于互联网怎样做网站推广桂林市市长
  • 成都项目网站建设版式设计优秀作品欣赏
  • 湖南省长沙建设工程造价站网站做数据可视化的网站
  • 去菲律宾做网站wordpress播放mp4
  • 中英文网站建站茌平网站建设公司
  • 品牌推广型网站成都航空公司官方网站
  • 备案信息查询系统网站建设优化陕西
  • 建设一个机械公司网站多少钱台州网站制作价格
  • 网站配置系统做赚钱的网站
  • 网站开发与服务器交互长沙人才招聘网官网
  • 做平面什么网站好用长沙网站建设好处
  • 无锡网站建设系统营销型网站建设团队
  • 合肥网站建设+一浪怎么识别网站是用什么语言做的
  • 引航博景网站做的好吗microsoft免费网站
  • 公司网站开发建设知名企业logo