I am trying to understand SoftAssert in TestNG. I have implemented own class extending Assertion class. As shown below my TestNG class contains only 1 test method which would fail. I dont understand since test assertion would fail then why onAssertSuccess is called.
Can some one help me in understanding this.
public class SoftAssert extends Assertion{
private final Map<AssertionError, IAssert> m_errors = Maps.newHashMap();
public SoftAssert() {
}
@Override
public void executeAssert(IAssert a)
{
try {
a.doAssert();
} catch (AssertionError ex) {
m_errors.put(ex, a);
onAssertFailure(a, ex);
}
}
public void assertAll()
{
if (!m_errors.isEmpty()) {
StringBuilder sb = new StringBuilder("The following asserts failed:\n");
boolean first = true;
for (Map.Entry<AssertionError, IAssert> ae : m_errors.entrySet()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(ae.getValue().getMessage());
}
throw new AssertionError(sb.toString());
}
}
@Override
public void onBeforeAssert(IAssert a)
{
Reporter.log("");
Reporter.log("ASSERT DESCRIPTION: " + a.getMessage());
}
@Override
public void onAssertFailure(IAssert a, AssertionError ex)
{
try {
Reporter.log("Test Case Validation :: FAILED");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onAfterAssert(IAssert a)
{
}
@Override
public void onAssertSuccess(IAssert a){
try{
Reporter.log("On Assert Success");
}catch(Exception e){
}
}
}
TestNG Class
public class TestSoftassert {
private SoftAssert s_Assert=null;
@BeforeMethod
public void bm(Method m){
s_Assert= new SoftAssert();
}
@Test
public void testA(){
s_Assert.assertTrue(false,"this is a failed test case");
s_Assert.assertAll();
}
}