一、spring-boot 整合junit测试类
1.引入junit依赖
<!-- springboot junit依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.编写测试类
package com.jiayun.service;
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 com.jiayun.App;
@RunWith(SpringJUnit4ClassRunner.class) // jar包中的类
@SpringBootTest(classes={App.class})// 指定spring-boot的启动类
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testInsertUser(){
userService.insertUser();
}
}
3.junit运行
链接:https://pan.baidu.com/s/1n8MO-8rNB91gjzbQXbuwLg
提取码:o5z2
复制这段内容后打开百度网盘手机App,操作更方便哦
二、spring-boot 整合testng测试
1.eclipse需要安装testng插件,eclipse两种直接安装的方法都不靠谱,反正我没装上。
用eclipse离线安装插件的方法
https://blog.youkuaiyun.com/hualf/article/details/78507274 验证OK
2.跟junit差不多的写测试方法就行了
https://blog.youkuaiyun.com/m0_37696252/article/details/82629168
对了,需要引入testng的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.13</version>
<scope>test</scope>
</dependency>
testng单元测试控制台输出中午编码有问题,解决方法
https://blog.youkuaiyun.com/jjj0817/article/details/70888732
在eclipse根目录下打开eclipse.ini文件,增加-Dfile.encoding=UTF-8 这一句话