一、介紹
junit是一款優秀的開源java單元測試框架,也是目前使用率最高最流行的測試框架,開發工具eclipse和idea對junit都有很好的支持,junit主要用于白盒測試和回歸測試。
<!--more-->
- 白盒測試:把測試對象看作一個打開的盒子,程序內部的邏輯結構和其他信息對測試人 員是公開的;
- 回歸測試:軟件或環境修復或更正后的再測試;
- 單元測試:最小粒度的測試,以測試某個功能或代碼塊。一般由程序員來做,因為它需要知道內部程序設計和編碼的細節;
junit github地址:https://github.com/junit-team
二、junit使用
開發環境:
- spring boot 2.0.4 release
- junit 4.12
- maven
- idea 2018.2
2.1 檢測junit依賴
如果是spring boot項目默認已經加入了junit框架支持,可在pom.xml中查看:
1
2
3
4
5
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> |
如果maven項目中沒有添加junit依賴,可參照如上代碼,手動添加。
2.2 基礎使用
簡單的測試代碼如下:
1
2
3
4
5
6
7
8
9
|
@runwith (springrunner. class ) @springboottest public class simpletest { @test public void dotest() { int num = new integer( 1 ); assert .assertequals(num, 1 ); } } |
在測試類中郵件運行項目,效果如下:
從控制臺可以看出測試通過了。
2.3 注解說明
2.3.1 注解列表
- @runwith:標識為junit的運行環境;
- @springboottest:獲取啟動類、加載配置,確定裝載spring boot;
- @test:聲明需要測試的方法;
- @beforeclass:針對所有測試,只執行一次,且必須為static void;
- @afterclass:針對所有測試,只執行一次,且必須為static void;
- @before:每個測試方法前都會執行的方法;
- @after:每個測試方法前都會執行的方法;
- @ignore:忽略方法;
2.3.2 超時測試
代碼如下,給test設置timeout屬性即可,時間單位為毫秒:
@test(timeout = 1000)
2.4 斷言測試
斷言測試也就是期望值測試,是單元測試的核心也就是決定測試結果的表達式,assert對象中的斷言方法:
- assert.assertequals 對比兩個值相等
- assert.assertnotequals 對比兩個值不相等
- assert.assertsame 對比兩個對象的引用相等
- assert.assertarrayequals 對比兩個數組相等
- assert.asserttrue 驗證返回是否為真
- assert.assertflase 驗證返回是否為假
- assert.assertnull 驗證null
- assert.assertnotnull 驗證非null
代碼示例如下:
1
2
3
4
5
6
7
8
9
10
11
|
@test public void dotest() { string[] string1 = { "1" , "2" }; string[] string2 = string1; string[] string3 = { "1" , "2" }; assert .assertequals(string1, string2); assert .assertequals(string2, string3); assert .assertsame(string1, string2); assert .assertsame(string2, string3); //驗證不通過,string2、string3指向的引用不同 } |
2.5 web模擬測試
在spring boot項目里面可以直接使用junit對web項目進行測試,spring 提供了“testresttemplate”對象,使用這個對象可以很方便的進行模擬請求。
web測試只需要進行兩步操作:
- 在@springboottest注解上設置“ebenvironment = springboottest.webenvironment.random_port”隨機端口;
- 使用testresttemplate進行post或get請求;
示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@runwith (springrunner. class ) @springboottest (webenvironment = springboottest.webenvironment.random_port) public class usercontrollertest { @autowired private testresttemplate resttemplate; @test public void getname() { string name = resttemplate.getforobject( "/name" , string. class ); system.out.println(name); assert .assertequals( "adam" , name); } } |
其中getforobject的含義代表執行get請求,并返回object結果,第二個參數設置返回結果為string類型,更多的請求方法:
- getforentity:get請求,返回實體對象(可以是集合);
- postforentity:post請求,返回實體對象(可以是集合);
- postforobject:post請求,返回對象;
2.6 數據庫測試
在測試數據操作的時候,我們不想讓測試污染數據庫,也是可以實現的,只需要添加給測試類上添加“@transactional”即可,這樣既可以測試數據操作方法,又不會污染數據庫了。
示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
|
@test @transactional public void savetest() { user user = new user(); user.setname( "adam" ); user.setage( 19 ); user.setpwd( "123456" ); userrepository.save(user); system.out.println( "userid:" + user.getid()); assert .asserttrue(user.getid()> 0 ); } |
執行效果如下:
我們可以看到id有了,也測試通過了,說明數據是添加是正常的,但查看數據庫發現數據里面是沒有這條數據的。
如果把“@transactional”去掉的話,數據庫就會正常插入了。
2.7 idea快速開啟測試
在idea里面可以快速的添加測試的方法,只需要在要測試的類里面右鍵選擇“goto”點擊“test”,選擇你需要測試的代碼,點擊生成即可,如果是windows 用戶可以使用默認快捷鍵“ctrl + shift + t”,效果如下圖:
選完方法之后,點擊ok按鈕,就生成了對應的測試代碼,用戶只需要完善框架里面的具體測試邏輯就可以了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000016907636