TestNG預期異常測試

在本教程中,我們將演示如何使用TestNG expectedExceptions來測試代碼中的預期異常拋出。

創建一個名稱爲 ExpectedExceptionTest 的 Maven 工程,其結構如下所示 -

TestNG預期異常測試

1. 運行時異常

此示例顯示如何測試運行時異常。 如果divisionWithException()方法拋出一個運行時異常 — ArithmeticException,它會獲得通過。

創建一個測試文件: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);
    }

}

運行上面代碼,得到以下結果 -

[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

2. 檢查異常

查看一個簡單的業務對象,保存和更新方法,如果有錯誤,則拋出自定義檢查的異常。

創建一個測試文件: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");

    }

}

測試預期異常的示例。創建一個主測試文件: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);
    }

}

運行上述單元測試代碼,最終將通過測試,得到如下結果 -

[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

示例中的其它幾個類(OrderNotFoundExceptionOrderSaveException等),請下載代碼參考。