spring boot 单元测试

本文介绍了Spring Boot单元测试的基本步骤。首先,需要引入相关依赖。然后,在Maven项目的test类中编写测试代码。测试类中,使用@Autowired注解注入要测试的类,确保其已准备好。注意,不同Spring Boot版本可能在细节上存在差异。
摘要由CSDN通过智能技术生成

    首先声明,本人菜鸟一只,如果有什么不正确的地方请指点指定。

    单元测试:对软件中最小可测单元进行测试检查,虽然属于测试范畴,但是一般都是由程序员进行操作。大家可以去百度下junit(回归测试框架),我这里就不做介绍了,首先spring boot 要引入依赖:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>
按住ctrl键,单击spring-boot-starter-test,可以看到以下内容:spring boot test 里面包含了
JUnit、Hamcrest 、Mockito

<description>Starter for testing Spring Boot applications with libraries including
   JUnit, Hamcrest and Mockito</description>  

 现在就简单的说一下spring boot 进行单元测试的简单步骤:

创建一个spring boot maven项目后,自动生成test类,在该test类里面添加代码,以个人demo作为示例:

@RunWith(SpringRunner.class)
@SpringBootTest(classes=GraduationApplication.class)
public class GraduationApplicationTests {
   @Autowired
   UserServerImplement userServerImplement;
   @Test
   public void contextLoads() {
      List<TblUser> users = userServerImplement.selectAllTblUser();
      Iterator user = users.iterator();
      while(user.hasNext()){
         System.out.println(user.next().toString());
      }
   }

}

说明:

自动织入(Autowired)的类为要测试的类,必须先写好

版本为1.5.8,不同版本可能有点差异

@RunWith(SpringRunner.class):使用springRunner来运行测试环境
@SpringBootTest(classes=GraduationApplication.class):启动类为GraduationApplication
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值