Write a simple TestNG test to validate that the sum of two numbers is as expected and print the result in the console.
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.
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);
}
}
Copyright © 2023 - Proleed Academy | All Rights Reserved.