testng 检查异常_TestNG异常– ExpectedExceptions,ExpectedExceptionsMessageRegExp

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);
	}

	@DataProvider
	public 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 zero
	at 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 检查异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值