testng 检查异常

Sometimes we want to test our methods for exceptions. TestNG allows us to perform exceptions testing using expectedExceptions and expectedExceptionsMessageRegExp parameters of @Test annotation.

有时我们想测试我们的方法是否有异常。 TestNG的允许我们进行的异常使用测试expectedExceptionsexpectedExceptionsMessageRegExp的参数@Test注释。

TestNG ExpectedExceptions (TestNG expectedExceptions)

Let’s say we have a simple utility method for dividing two numbers.

假设我们有一个简单的实用方法来除以两个数字。

package com.journaldev.exceptions;public class MathUtils {public int divide(int x, int y) {return x / y;}
}

We know that if we try to divide a number by 0, it will throw java.lang.ArithmeticException with message as / by zero.

我们知道,如果尝试将数字除以0,则它将抛出java.lang.ArithmeticException ,消息为/ by zero

Let’s write a TestNG Test class to test this exception scenario.

让我们编写一个TestNG Test类来测试这种异常情况。

package com.journaldev.exceptions;import org.testng.annotations.Test;import static org.testng.Assert.assertEquals;import org.testng.annotations.DataProvider;public class MathUtilsTest {@Test(dataProvider = "dp", expectedExceptions = {ArithmeticException.class })public void f(Integer x, Integer y) {MathUtils mu = new MathUtils();assertEquals(mu.divide(x, y), x / y);}@DataProviderpublic Object[][] dp() {return new Object[][] { new Object[] { 4, 0 } };}
}

We get the following output when we execute above test class.

当我们执行上面的测试类时,我们得到以下输出。

[RemoteTestNG] detected TestNG version 6.14.3
PASSED: f(4, 0)

If we remove the expectedExceptions attribute from the @Test annotation, then our test case will fail with below message.

如果我们去掉expectedExceptions从属性@Test注释,那么我们的测试案例将失败,下面的消息。

[RemoteTestNG] detected TestNG version 6.14.3
FAILED: f(4, 0)
java.lang.ArithmeticException: / by zeroat com.journaldev.exceptions.MathUtils.divide(MathUtils.java:6)at com.journaldev.exceptions.MathUtilsTest.f(MathUtilsTest.java:13)

expectedExceptions takes value as class array, so below value will work fine too.

expectedExceptions将value作为类数组,因此低于value也可以正常工作。

expectedExceptions = {ArithmeticException.class, NullPointerException.class }

TestNG测试ExpectedExceptionsMessageRegExp (TestNG Test expectedExceptionsMessageRegExp)

We can extend our test method to look for exception message too. Test annotation expectedExceptionsMessageRegExp uses regular expression. Let’s see few examples of how we can use it.

我们也可以扩展测试方法以查找异常消息。 测试注释expectedExceptionsMessageRegExp使用正则表达式。 让我们看一些如何使用它的例子。

@Test(dataProvider = "dp", expectedExceptions= {ArithmeticException.class},expectedExceptionsMessageRegExp="/ by zero")public void f(Integer x, Integer y) {MathUtils mu = new MathUtils();assertEquals(mu.divide(x, y), x/y);}

Above test method will pass only when ArithmeticException is thrown with the exact message as “/ by zero”.

仅当抛出ArithmeticException且确切消息为“ / by zero”时,上述测试方法才能通过。

[RemoteTestNG] detected TestNG version 6.14.3
PASSED: f(4, 0)

Let’s change the message so that our test will fail.

让我们更改消息,以使我们的测试失败。

@Test(dataProvider = "dp", expectedExceptions= {ArithmeticException.class}, expectedExceptionsMessageRegExp="by zero")
public void f(Integer x, Integer y) {MathUtils mu = new MathUtils();assertEquals(mu.divide(x, y), x/y);
}

Our test will fail with following console output.

我们的测试将失败,并显示以下控制台输出。

[RemoteTestNG] detected TestNG version 6.14.3
FAILED: f(4, 0)
org.testng.TestException:
The exception was thrown with the wrong message: expected "by zero" but got "/ by zero"at org.testng.internal.ExpectedExceptionsHolder.wrongException(ExpectedExceptionsHolder.java:71)

Let’s see how we can use the regular exception when we are not sure of the exact message of exception.

让我们看看在不确定确切的异常消息时如何使用常规异常。

@Test(dataProvider = "dp", expectedExceptions= {ArithmeticException.class},expectedExceptionsMessageRegExp=".* by zero*.")
public void f(Integer x, Integer y) {MathUtils mu = new MathUtils();assertEquals(mu.divide(x, y), x/y);
}

This test will pass because the regex provided by us will match with the exception message being thrown.

该测试将通过,因为我们提供的正则表达式将与抛出的异常消息匹配。

GitHub Repository.GitHub Repository下载示例代码。

翻译自: https://www.journaldev.com/21331/testng-exceptions-expectedexceptions-expectedexceptionsmessageregexp

testng 检查异常

testng 检查异常_TestNG异常– ExpectedExceptions,ExpectedExceptionsMessageRegExp相关推荐

  1. java怎么捕捉除数异常_Java异常的捕获及处理---小总结

    一:异常的基本概念 二:异常的基本处理格式 三:异常的继承结构 四:Java的异常处理机制 五:throws和throw关键字的作用 六:Exception和RunntimeException的区别 ...

  2. C++ 面向对象(三)异常 :异常概念、异常的匹配规则、异常安全、异常体系

    目录 C语言传统的错误处理方法 异常的概念 异常的匹配规则 异常安全 异常规范 异常体系 C++标准库的异常体系 自定义异常体系 C语言传统的错误处理方法 在C语言中,因为没有异常这个机制,所以出现错 ...

  3. 多态的概念、对象上下转型、多态的应用、异常(异常概念、异常分类、java异常处理机制、try...catch...finally、throw和throws、自定义异常)

    多态的概念: 多态性是指同一个操作作用于某一类对象,可以有不同的解释,产生不同的执行结果. 多态存在的三个必要条件: ① 需要存在继承或实现关系 ② 同样的方法调用而执行不同的操作.运行不同代码(重写 ...

  4. assertionerror是什么异常_php7异常与错误处理和自定义异常

    7.2版本异常与错误的概述 什么叫做异常? 异常是指程序运行中不符合预期情况以及与正常流程不同的状况. 比如你链接数据库,在参数都写上去的条件下,发现链接不上去,这就属于不符合预期. 可以被 try- ...

  5. java 到异常_java编程中遇到的异常以及异常的一些处理

    n 异常的概念 程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常.异常发生时,是任程序自生自灭,立刻退出终止,还是输出错误给用户? 比如除法运算.读写文件操作,都可能 ...

  6. 【02】Java进阶:09-冒泡排序、选择排序、二分查找、异常、异常的产生和处理、自定义异常、多线程

    day09[排序算法.异常.多线程基础] 今日内容 冒泡排序 选择排序 二分查找 异常处理 多线程基础 教学目标 能够理解冒泡排序的执行原理 能够理解选择排序的执行原理 能够理解二分查找的执行原理 能 ...

  7. C++异常(异常的基本语法、栈解旋unwinding、异常接口声明、异常变量的生命周期、异常的多态使用、C++系统标准异常库)

    文章目录 1 异常的基本概念 1.1 C语言中的异常处理 1.2 C++中的异常处理 1.3 异常严格类型匹配 2 栈解旋(unwinding) 3 异常的接口声明[C++11已废弃] 4 异常变量的 ...

  8. 运行时异常一般异常的区别

    Throwable 是所有 Java 程序中错误处理的父类 ,有两种资类: Error 和 Exception . Error :表示由 JVM 所侦测到的无法预期的错误,由于这是属于 JVM 层次的 ...

  9. 西软服务器显示数据库异常,异常:数据库异常

    异常:数据库异常 02/21/2013 本文内容 本文演示如何处理数据库异常.大多数材料本文中应用您使用的是了开放式数据库连接 (odbc) 的 MFC 类或数据访问对象的 MFC 类一起使用 (DA ...

最新文章

  1. FreeMarker学习
  2. 关于Dalvik虚拟机你需要知道的15个问题
  3. 王炸!Azure云助力.NET6现高光时刻(VS2022实战尝鲜)
  4. Java Lambdas和低延迟
  5. freemarker -基本概念
  6. USACO详细介绍 全球中小学生均可参加
  7. ASIHTTPRequest-断点续传需要原网站支持!
  8. SCPPO(四):框架的学习
  9. Centos-显示文件类型-file
  10. excel去重_提高数据处理效率的10个excel小技巧
  11. SEO优化中关键词密度和布局的重要性
  12. 超详细的八种RAID磁盘阵列原理及优缺点
  13. h5 换脸 php,DIY海报H5案例|换脸show颜值
  14. 树莓派能做什么呢?如何使用树莓派
  15. Java工程师是做什么的?学习java能干什么?
  16. USB连接默认更改为传输文件
  17. Windows10 会不会成为微软的新起点?
  18. 联手腾讯八百客CRM实现“本土化”弯道超车
  19. 知乎1w视频播放能赚多钱?今天来大揭秘
  20. linux软件都能安装在Uos,在uos 20/deepin V20中添加华为软件源,就能打开应用商店和安装软件...

热门文章

  1. [转载] python中sort,sorted,reverse,reversed的区别
  2. [转载] python3 numpy函数_Python numpy总结(3)——常用函数用法
  3. [转载] 为什么this()和super()必须是构造函数中的第一条语句?
  4. Vue.js 学习笔记 八 v-for
  5. 序列的修改、散列和切片
  6. 对着IDEA 配置方式
  7. LeetCode447. Number of Boomerangs
  8. 使用图片拉伸resizableImageWithCapInsets
  9. 在 ASP.NET MVC 中创建自定义 HtmlHelper
  10. yolo之---非极大值抑制