Assertequals TestNG

Share

Assertequals Testng

In TestNG, the assertion assertEquals is used to compare the expected and actual values in a test case. It checks if the two values are equal and reports a failure if they are not. Here’s how you can use assertEquals in TestNG:

  1. Import the required TestNG assertion class:
java
import org.testng.Assert;
  1. Use assertEquals in your test method to compare the expected and actual values. The syntax is as follows:
java
Assert.assertEquals(actualValue, expectedValue);
  1. Here’s an example test method that uses assertEquals:
java
import org.testng.Assert; import org.testng.annotations.Test; public class MyTest { @Test public void testAddition() { int actualResult = 2 + 2; int expectedResult = 4; Assert.assertEquals(actualResult, expectedResult, "Addition result mismatch"); } }

In the above example, the assertEquals method compares the actualResult (which is the result of adding 2 and 2) with the expectedResult of 4. If the two values are not equal, a failure will be reported with the provided error message “Addition result mismatch”.

The assertEquals method is overloaded and supports different data types, including primitives, objects, arrays, and more. You can also add an optional error message as the last parameter to provide more context in case of assertion failures.

TestNG provides several other assertion methods like assertTrue, assertFalse, assertNull, assertNotNull, and more, which you can use based on your specific testing needs.

Using assertions in your test methods helps in validating the expected behavior and provides feedback on test failures, enabling you to identify issues and fix them promptly.

Demo Day 1 Video:

 
You can find more information about Selenium in this Selenium Link

 

Conclusion:

Unogeeks is the No.1 IT Training Institute for Selenium Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on  Selenium here – Selenium Blogs

You can check out our Best In Class Selenium Training Details here – Selenium Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook:https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *