SpringBoot整合junit测试案例

1.之前开发项目是不要求写单测的,最近公司管理严格需要对开发的功能编写单测,所以在此记录下springboot对junit的整合以及使用的方式

2.引入需要用到的依赖jar包,一般创建好springboot项目都会自带test依赖

3.一般我们新建的springboot项目都带有测试包,我们直接使用,在里面编写测试类即可

 4.因为项目中可能会存在很多测试类,那么就会存在很多注解重复被添加的冗余,因此我们写一个基类,其他测试类只需要继承基类就行,基类名字就叫做BaseTestClass,如下:

package com.example.mybatisplus;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@SpringBootTest
@WebAppConfiguration
@RunWith(SpringRunner.class)
public class BaseTestClass {

    @Before
    public void init(){
        System.out.println("before");
    }

    @After
    public void after(){
        System.out.println("after");
    }
}

 5.然后就可以编写测试类了,测试类里面注入service层,然后通过断言进行返回数据的判断,下面就是我测试的一个查询接口的测试类:

package com.example.mybatisplus;

import com.example.mybatisplus.entity.Employee;
import com.example.mybatisplus.service.EmployeeService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.assertEquals;

public class OperateDataTest extends BaseTestClass{
    @Autowired
    private EmployeeService employeeService;

    @Test
    public void testQueryById(){
        Employee employee = employeeService.selectById("2");
        assertEquals("Jerry",employee.getLastName());
        assertEquals("jerry@qq.com",employee.getEmail());
    }
}

注意:由于业务逻辑层都是要求写在service层,所以我们这里就注入了service进行测试。

那有同学要问了,我直接测试controller层可以吗?答案肯定是可以的,下面我就演示一下直接注入controller层,其实和service是一样的,如下所示:

package com.example.mybatisplus;

import com.example.mybatisplus.controller.MybatisPlusController;
import com.example.mybatisplus.entity.Employee;
import com.example.mybatisplus.service.EmployeeService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.junit.Assert.assertEquals;

public class OperateDataTest extends BaseTestClass{
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private MybatisPlusController mybatisPlusController;

    @Test
    public void testQueryById(){
        Employee employee = employeeService.selectById("2");
        assertEquals("Jerry",employee.getLastName());
        assertEquals("jerry@qq.com",employee.getEmail());
    }

    @Test
    public void testController(){
        List<Employee> employeeList = mybatisPlusController.queryList();
        System.out.println("employee="+employeeList.get(0));

    }
}

项目代码已上传csdn,下载链接:springboot整合junit测试用例demo-Java文档类资源-CSDN下载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

酒书

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

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

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

打赏作者

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

抵扣说明:

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

余额充值