SpringBoot边学边用(七)SpringBoot单元测试

 

编写单元测试可以帮助开发人员编写高质量的代码,减少Bug,便于重构;Spring Boot提供了一些注解和工具去帮助开发者测试他们的应用,在Spring Boot中开启单元测试只需要引入spring-boot-starter-test即可,其包含了一些主流的测试库

添加项目依赖

在项目pom.xml文件<dependencies></dependencies>标签内添加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

创建单元测试类

在项目src/test目录下,存在新建项目时自动创建的入口类的测试类,接下来我们测试CustomerServiceImpl中的函数方法,所以在demo下新建包service/impl,然后创建对应的CustomerServiceImplTests测试类

 

添加注解

一个标准的SpringBoot测试用例应该包含两个注解:

  • @SpringBootTest:意思是带有SpringBoot支持的引导程序

  • @RunWith(SpringRunner.class):告诉JUnit运行使用Spring的测试支持

package com.syg.demo.service.impl;
​
import com.syg.demo.dao.CustomerMapper;
import com.syg.demo.po.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
​
@SpringBootTest
@RunWith(SpringRunner.class)
public class CustomerServiceImplTests {
    
    @Autowired
    private CustomerMapper customerMapper;
    //查询客户
    //要测试的方法上添加@Test注解
    @Test
    public void findCustomerById(){
        Customer custom = this.customerMapper.selectByPrimaryKey(1);
        System.out.println(custom);
    }
}

注意:单元测试方法为void类型,这里将查询结果在控制台输出

运行单元测试方法

在带有@Test注解的左侧可以直接点击运行

可以在控制台中查看到输出结果,即通过主键查询数据方法成功

 

阅读更多技术文章,及时获取内容更新,请扫码关注微信公众号-大数据School!

欢迎评论区留下你的精彩评论~ 觉得文章不错可以分享到朋友圈让更多的小伙伴看到哦~

同学!在看一下呗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值