spring-boot 整合junit測試類 和 spring-boot整合整合testng
阿新 • • 發佈:2019-01-03
一、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離線安裝外掛的方法
2.跟junit差不多的寫測試方法就行了
對了,需要引入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單元測試控制檯輸出中午編碼有問題,解決方法
在eclipse根目錄下開啟eclipse.ini檔案,增加-Dfile.encoding=UTF-8 這一句話