javaWeb(一)之單元測試篇
一、junit的使用
* 單元測試
* 測試物件是 是一個類中的方法
* juint不是javase的一部分,想要使用匯入jar包
** 但是,在myeclipse中自帶了junit的jar包
* 首先junit版本 3.x 4.x
* 單元測試方法時候,方法命名規則 public void 方法名() {}
* 使用註解方式執行測試方法, 在方法的上面
** @Test:表示方法進行單元測試
--- @Test
public void testAdd1() {
TestJunit test01 = new TestJunit();
test01.testAdd(2, 3);
}
- 選中方法名稱,右鍵執行 點選run as --- junit test
- 當出現綠色條,表示方法測試通過
- 當出現了紅棕色條,表示方法測試不通過
--- 要執行類中的多個測試方法,點選類中的其他位置,run as --- junit test
** @Ignore :表示這個方法不進行單元測試
** @Before: 在每個方法執行執行
** @After:在每個方法之後執行
** 斷言(瞭解)
- Assert.assertEquals("測試期望的值", "方法執行的實際的值")
二、單元測試錯誤集合
一、假如出現method initializationerror not found,open the test class錯誤
首先、註釋所有其他又@test註解的方法
然後,檢查,你需要使用單元測試的方法是否新增@test註解,並且是否符合上述方法命名規則。
注意檢查是否匯入正確的包,