TestNG Assert类方法是怎样的

TestNG Assert类方法是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

package com.testng.examples;

import org.testng.Assert;

import org.testng.annotations.Test;

public class AssertTest {

@Test

public void test() {

/**

 * Assert#assertEquals

 * 

 * 1.assertEquals方法可对java中所有数据类型进行断言比较。

 * 2.基本数据类型直接进行值比较进行断言

 * 3.包装类及自定义继承自Object的数据类型则使用equals方法进行比较

 * 4.Set类型数据使用类的equals方法进行比较(Set类已复写Object的equals方法)

 * 5.其他Collection类型数据,比如List类型数据,则按顺序遍历所有元素,并使用equals方法进行比较

 * 6.数组类型数据,遍历数组中各元素,并通过元素类型的equals方法进行比较,如果数组元素为基本数据类型则使用值比较

 */

/*

Assert.assertEquals(actual, expected);

Assert.assertEquals(actual, expected, message);

Assert.assertEquals(actual, expected, delta);

Assert.assertEquals(actual, expected, delta, message);

 */

//用于对map数据类型进行比较,该方法会对map元素中数组各元素按顺序比较

//Assert.assertEqualsDeep(null, null);

//用于对set数据类型进行比较,该方法会遍历set元素中所有元素,且Set数据为数组类型时,会对数组各元素按顺序比较

//Assert.assertEqualsDeep(actual, expected, message);

String[] a = new String[]{"a3","a1","a2"};

String[] a1 = new String[]{"a3","a1","a2"};

String[] b = new String[]{"a1","a2","a3"};

Assert.assertEquals(a, a1);

Assert.assertNotEquals(a, b);

System.out.println(a.equals(a1));//true

//断言两个数组包含相同元素,并且忽略数组元素的排列顺序

Assert.assertEqualsNoOrder(a, b);

//断言两个bool类型数据

Assert.assertFalse(false);

Assert.assertTrue(true);

//断言Object类型数据是否为null

Assert.assertNull(null);

Assert.assertNotNull(new Object());

//断言两个对象是否引用同一个对象

//ssert.assertSame(new Integer(1), new Integer(1));//failed

Assert.assertNotSame(new Integer(1), new Integer(1));//success

//断言一段可执行程序有异常抛出

Assert.assertThrows(()->{throw new RuntimeException();});//success

//Assert.assertThrows(NullPointerException.class, ()->{throw new RuntimeException();}); //failed

//自定义断言失败

//Assert.fail("Test execution failed cased by somthing reason.");

}

}

关于TestNG Assert类方法是怎样的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。