junit的使用
* 單元測試
-
測試對象是 是一個類中的方法
-
juint不是javase的一部分,想要使用導入jar包
** 但是,在myeclipse中自帶了junit的jar包 -
首先junit版本 3.x 4.x
注意:
單元測試方法時候,方法命名規則 public void 方法名() {}
@Test
public void testAdd1() {
TestJunit test01 = new TestJunit();
test01.testAdd(2, 3);
}
-
使用註解方式運行測試方法, 在方法的上面
** @Test:表示方法進行單元測試
選中方法名稱,右鍵運行 點擊run as — junit test
- 當出現綠色條,表示方法測試通過
- 當出現了紅棕色條,表示方法測試不通過要運行類中的多個測試方法,點擊類中的其他位置,run as — junit test
** @Ignore :表示這個方法不進行單元測試
** @Before: 在每個方法執行運行
** @After:在每個方法之後運行
** 斷言
- Assert.assertEquals(“測試期望的值”, “方法運行的實際的值”)