Validate the TestNG Test Case using the Assertion

  • Estimate Completion Time
    not found 30 minutes
  • Difficulty Level:

Write a simple TestNG test to validate that the sum of two numbers is as expected and print the result in the console.

Steps to follow

Create a simple method addition to add two numbers.

Create a test method testaddition() having @Test annotation indicating that this is a TestNG test.

Inside this method, take two numbers and validate if their sum is equal to the expected sum.

If the sum is not equal to the expected sum, the test will fail, and “The sum is not as expected” message will display.

Solution

package practice;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public class Exercise1 {

	public int addition(int a, int b) {
		int c = a + b;
		return c;
	}

	@Test
	public void testaddition() {
		int a = 50;
		int b = 135;
		int expectedsum = 185;
		int actualsum = addition(a, b);
		assertEquals(expectedsum, actualsum, "The sum is not as expected. Expected sum is  " + expectedsum + “ and actual sum is " + actualsum);
		System.out.println("The expected sum is " + expectedsum + " and actual sum is " + actualsum);
	}
}
TestNG Practice Exercises - QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.