在本教程中,我們將演示如何使用TestNG expectedExceptions來(lái)測(cè)試代碼中的預(yù)期異常拋出。
創(chuàng)建一個(gè)名稱(chēng)為 ExpectedExceptionTest 的 Maven 工程,其結(jié)構(gòu)如下所示 -
此示例顯示如何測(cè)試運(yùn)行時(shí)異常。 如果divisionWithException()
方法拋出一個(gè)運(yùn)行時(shí)異常 — ArithmeticException
,它會(huì)獲得通過(guò)。
創(chuàng)建一個(gè)測(cè)試文件:TestRuntime.java ,其代碼如下所示 -
package com.yiibai;
import org.testng.annotations.Test;
public class TestRuntime {
@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException() {
int i = 1 / 0;
System.out.println("After division the value of i is :"+ i);
}
}
運(yùn)行上面代碼,得到以下結(jié)果 -
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-717368002\testng-customsuite.xml
PASSED: divisionWithException
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@1b40d5f0: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6ea6d14e: 47 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4563e9ab: 0 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 15 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2aaf7cc2: 31 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@45c8e616: 0 ms
查看一個(gè)簡(jiǎn)單的業(yè)務(wù)對(duì)象,保存和更新方法,如果有錯(cuò)誤,則拋出自定義檢查的異常。
創(chuàng)建一個(gè)測(cè)試文件:OrderBo.java ,其代碼如下所示 -
package com.yiibai;
public class OrderBo {
public void save(Order order) throws OrderSaveException {
if (order == null) {
throw new OrderSaveException("Order is empty!");
}
// persist it
}
public void update(Order order) throws OrderUpdateException, OrderNotFoundException {
if (order == null) {
throw new OrderUpdateException("Order is empty!");
}
// if order is not available in database
throw new OrderNotFoundException("Order is not exists");
}
}
測(cè)試預(yù)期異常的示例。創(chuàng)建一個(gè)主測(cè)試文件:TestCheckedException.java ,其代碼如下所示 -
package com.yiibai;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestCheckedException {
OrderBo orderBo;
Order data;
@BeforeTest
void setup() {
orderBo = new OrderBo();
data = new Order();
data.setId(1000);
data.setCreatedBy("maxsu");
}
@Test(expectedExceptions = OrderSaveException.class)
public void throwIfOrderIsNull() throws OrderSaveException {
orderBo.save(null);
}
/*
* Example : Multiple expected exceptions Test is success if either of the
* exception is thrown
*/
@Test(expectedExceptions = { OrderUpdateException.class, OrderNotFoundException.class })
public void throwIfOrderIsNotExists() throws OrderUpdateException, OrderNotFoundException {
orderBo.update(data);
}
}
運(yùn)行上述單元測(cè)試代碼,最終將通過(guò)測(cè)試,得到如下結(jié)果 -
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--154903203\testng-customsuite.xml
PASSED: throwIfOrderIsNotExists
PASSED: throwIfOrderIsNull
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@1b40d5f0: 16 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6ea6d14e: 109 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4563e9ab: 16 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2aaf7cc2: 31 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@45c8e616: 0 ms
示例中的其它幾個(gè)類(lèi)(OrderNotFoundException
,OrderSaveException
等),請(qǐng)下載代碼參考。