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");
}
}