測試分類:
- 黑盒測試:不需要寫代碼,給輸入值,看程序是否能夠輸出期望的值。
- 白盒測試:需要寫代碼的。關注程序具體的執行流程。
Junit使用:白盒測試
步驟:
-
定義一個測試類(測試用例),建議:
- 測試類名:被測試的類名Test CalculatorTest
- 包名:xxx.xxx.xx.test top.onefine.test
-
定義測試方法:可以獨立運行,建議:
- 方法名:test測試的方法名 testAdd()
- 返回值:void
- 參數列表:空參
-
給方法加
@Test
-
導入junit依賴環境
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
判定結果:
- 紅色:失敗
- 綠色:成功
- 一般會使用斷言操作來處理結果:
Assert.assertEquals(期望的結果, 運算的結果)
;
補充:
@Before
:init() 修飾的方法會在測試方法之前被自動執行,常用於資源申請@After
:destroy() 修飾的方法會在測試方法執行之後自動被執行,常用於釋放資源