SpringBoot的单元测试

SpringBoot测试

1、引入相关依赖

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

2、我们发现 SpringRunner 底层使用的是 JUnit,Junit这种老技术,相信很多人都相当的熟悉了,SpringBoot 2.X 默认使用Junit4

直接看测试类吧!

package testMybatis;

import com.room19.datasource.Room19DatasourceMybatisApplication;
import com.room19.datasource.dao.studyspringcloud.DeptMapper1;
import com.room19.datasource.dao.studyspringcloud2.DeptMapper2;
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.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/*
 * @Author kaico
 * @Date 8:44 2020/8/23
 * @Description //TODO 单元测试类
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { Room19DatasourceMybatisApplication.class})//配置启动类
public class TestDatasource {

    @Autowired
    private DeptMapper1 deptMapper1;
    @Autowired
    private DeptMapper2 deptMapper2;


    @Test
    public void testOne(){
        System.out.println("test hello 1");
    }

    @Test
    public void testTwo(){
        List<Map<String, Object>> list = deptMapper1.list();
        String s = list.toString();
        System.out.println("集合输出:" + s);
        System.out.println("test hello 2");
    }


    /*
     * @Author kaico
     * @Date 8:56 2020/8/23
     * @Description //TODO 测试多数据源
     * @Param []
     * @return void
     **/
    @Test
    public void testThree(){
        int count = deptMapper1.insertDept("凯凯", "studyspringcloud");
        int count2 = deptMapper2.insertDept("欢欢", "studyspringcloud2");
        System.out.println("studyspringcloud插入数据条数:" + count);
        System.out.println("studyspringcloud2插入数据条数:" + count2);
        System.out.println("test hello 3");
    }

    /*
     * @Author kaico
     * @Date 8:56 2020/8/23
     * @Description //TODO 测试多数据源的事务
     * @Param []
     * @return void
     **/
    @Test
    @Transactional(rollbackFor = Exception.class)
    public void testFour(){
        int count = deptMapper1.insertDept("凯凯5", "studyspringcloud");
        System.out.println("studyspringcloud插入数据条数:" + count);
        List<Map<String, Object>> list = deptMapper1.list();
        String s = list.toString();
        System.out.println("集合输出:" + s);
        int one = 1/0;
        System.out.println("test hello 3");
    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值