In a testing method if we add some assert statement like this
@Test
public void regClass(){
System.out.println("A");
Assert.assertEquals("Good","Goodx");
System.out.println("B");
}
Then after second statement test will fail and B will not print. This is hard assertion and can we used if we want to check something really important and want the test to fail after that.
But what if we are just checking something simple like the title of a page with the expected result. We don’t want the entire test to fail after that. we just want an error message and want to continue the other statements of that test annotation function after that.
So for that we use soft assertions.
Example of soft assertion is below. We need to create an object of SoftAssert before running the test.So instead of using assert class we use SoftAssert class and its methods.
Please note we need to add sa.assertAll in the end to fail the test in the end.
@Test
public void regClass(){
SoftAssert sa = new SoftAssert();
System.out.println("A");
sa.assertEquals("Good","Bad");
System.out.println("B");
sa.assertAll();
}
So the above test will print A and B but in the end will say that test has failed as assertion has failed. So basically it will not stop the execution of entire test if one assertion fails.