@Test: 將一個 普通的方法修飾成爲一個測試方法
@Test(timeout = 3000)
1、終止死循環,當達到設定的值,結束循環
@Test(timeout = 2000)
public void TestWhile(){
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
while (true) {
System.out.println("終止死循環,當達到設定的值,結束循環");
}
}
2、測試方法執行時間是否在預期時間內
@Test(timeout = 3000)
public void TestWhile(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// while (true) {
// System.out.println("終止死循環,當達到設定的值,結束循環");
// }
}
3、處理運行時異常
@Test(expected=ArithmeticException.class)
public void TestDivide(){
assertEquals(3, new Calculate().divide(9, 0));
}
2秒在3秒內,測試通過
@BeforeClass: 他會在所有的方法運行前被執行,static修飾,測試類被運行就會被加載,在內存中只有一份實例,適合加載配置文件。
@AfterClass 他會在所有方法運行結束後被執行,static修飾,用於資源的清理,如關閉數據庫。
@Before:會在每一個測試方法被運行前執行一次
@After 會在每個測試方法被運行後執行一次
@Ignore 所修飾的測試方法會被測試運行器忽略
@RunWith: 可以修改測試運行器 org.junit.runner.Runner(默認運行器)
測試套件就是組織測試類一起運行的
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculateTest.class,CalculateTest2.class})
public class SuiteTest {
}
寫一個作爲測試套件的入口類,這個類裏不包含其他的方法。
更改測試運行器Suite.class.
將要測試的類作爲數組傳入到Suite.SuiteClasses({測試類.class數組})
eg:@RunWith(Suite.class) @Suite.SuiteClasses({TaskTest1.class,TaskTest2.class,TaskTest3.class})