鍍金池/ 教程/ Java/ TestNG預(yù)期異常測試
TestNG參數(shù)測試(DataProvider)
TestNG配置注解實(shí)例
TestNG預(yù)期異常測試
TestNG + Selenium負(fù)載測試
TestNG參數(shù)化測試
TestNG參數(shù)測試(XML和DataProvider)
TestNG配置注釋
TestNG套件測試
TestNG依賴測試
TestNG忽略測試
TestNG忽略測試
TestNG分組測試
TestNG預(yù)期異常測試
TestNG + Selenium
TestNG參數(shù)測試實(shí)例
TestNG超時(shí)測試
TestNG Hello World入門示例
JUnit 4 Vs TestNG比較
TestNG + Spring集成示例
TestNG分組測試
TestNG教程
TestNG超時(shí)測試
JUnit 4 Vs TestNG比較
TestNG基本注解
TestNG依賴性測試
TestNG套件測試
TestNG + Spring集成測試

TestNG預(yù)期異常測試

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

創(chuàng)建一個(gè)名稱為 ExpectedExceptionTest 的 Maven 工程,其結(jié)構(gòu)如下所示 -

1. 運(yùn)行時(shí)異常

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

創(chuàng)建一個(gè)測試文件: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

2. 檢查異常

查看一個(gè)簡單的業(yè)務(wù)對象,保存和更新方法,如果有錯(cuò)誤,則拋出自定義檢查的異常。

創(chuàng)建一個(gè)測試文件: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");

    }

}

測試預(yù)期異常的示例。創(chuàng)建一個(gè)主測試文件: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)行上述單元測試代碼,最終將通過測試,得到如下結(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è)類(OrderNotFoundException,OrderSaveException等),請下載代碼參考。


上一篇:TestNG教程下一篇:TestNG依賴測試