1. Affirmation
Assertion is a method in TestNG to determine whether the expected and actual results match. If they match, the program continues, and if they don’t match, an exception will be run directly.
Before using the assertion, we must introduce the dependency of TestNG
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
</dependency>
2. Usage
assertTrue determines whether it is true. assertFalse determines whether it is false. assertSame Determines whether the reference addresses are equal. assertNotSame Determines whether the reference addresses are not equal. assertNull judge whether it is null assertNotNull judge whether it is not null assertEquals determines whether they are equal. Objects of type Object need to implement hashCode and equals methods. Objects of collection type Collection/Set/Map also need to implement hashCode and equals methods. It is fun when three double parameters are used. The first two doubles are equal, or the previous The difference between the two doubles is less than the third double value passed in, that is, when the offset is less than how much, it is considered equal. assertNotEquals to determine whether they are not equal assertEqualsNoOrder determines whether the ignored order is equal
3. Examples
public class TestAssert {
@Test
public void testAssertEqual(){
Assert.assertEquals("aaa","aaa");
}
@Test
public void testAssertNull(){
Assert.assertNull(null,"error message");
}
@Test
public void testAssertTrue(){
Assert.assertTrue(true,"error message");
}
}