The Assertion results are not so good. They are getting appended for each assertions followed after the 1st Assertion statement. The sa.assertAll should fail the test method as ONLY Test2 & Test5 assertions should pass and rest all should fail with correct print statements in console. But with my code, the statements are getting appended.
package com.roughwork;
import org.testng.asserts.SoftAssert;
public class AssertionDemo {
SoftAssert sa;
public AssertionDemo(SoftAssert sa) {
this.sa = sa;
}
public void assertTrue(boolean condition, String msg) {
//sa = new SoftAssert();
try {
sa.assertTrue(condition);
sa.assertAll();
System.out.println(msg);
}catch(AssertionError ae) {
System.out.println("Assertion Failed: "+msg+ "\n" +ae.getLocalizedMessage());
}
}
public void assertEquals(Object actual, Object expected, String msg) {
//sa = new SoftAssert();
try {
sa.assertEquals(actual, expected);
sa.assertAll();
System.out.println(msg);
}catch(AssertionError ae) {
System.out.println("Assertion Failed: "+msg+ "\n" +ae.getLocalizedMessage());
}
}
public SoftAssert getTheAssertObj() {
return sa;
}
}
AssertionExample.java
package com.roughwork;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
public class AssertionExample {
AssertionDemo ademo;
@BeforeClass
public void beforeClass() {
System.out.println("Before Class Method is executed");
}
@Test
public void f() {
SoftAssert sa = new SoftAssert();
ademo = new AssertionDemo(sa);
ademo.assertEquals(true, false, "Test1");
ademo.assertEquals(false, false, "Test2");
ademo.assertTrue('a'=='b', "Test3");
ademo.assertEquals(5, 6, "Test4");
ademo.assertEquals("Mayank", "Mayank", "Test5");
ademo.getTheAssertObj().assertAll();
}
@AfterClass
public void afterClass() {
System.out.println("After Class Method is executed");
}
}

