Springboot单元测试

关于junit基本使用可以查看文章

https://blog.csdn.net/qq_34491508/article/details/131324082

一、Springboot整合测试

1、概述

对于简单易懂的小项目而言,可以不适用单元测试对平时开发没有什么影响,但是对于大型项目,单纯的依赖 “手点功能测试”, 那简直就是灾难,Springboot如何测试?

Springboot提供一系列测试工具集以及测试注解供我们测试。

  • spring-boot-test提供核心测试能力
  • spring-boot-test-autoconfigure提供测试的一些自动配置
  • 我们只需要导入spring-boot-starter-test即可整合测试
  • spring-boot-starter-test默认集成了如下库供我们测试使用
  1. JUnit
  2. Spring Test
  3. AssertJ
  4. Hamcrest
  5. Mockito
  6. JSONassert
  7. JsonPath

2、基本使用

创建项目引入依赖

 <!--springboot程序测试依赖,如果是自动创建项目默认添加-->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope> <!--当前依赖只参与测试范围,不参与打包-->
     </dependency>

项目结构

 

 注:主程序和主测试类所属的包一样

测试使用

@SpringBootTest//标注次注解说明可以测试spring容器中的所有组件的功能
class SpringboottestApplicationTests {

	@Autowired
	UserMapper userMapper;
	
	@Test
	void contextLoads() {
		List<User> list = userMapper.selectBySql("select * from user ");
		System.out.println(list.toString());
	}

}

3、测试注解

下面是junit5测试相关的注解

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序三两行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值