What is SoftAssert in TestNG (Selenium)?
To overcome the disadvantage that HardAssert has (it stops the execution of test after assertion error has occurred), a customized error handler is provided by TestNG, called SoftAssert.
SoftAssert – collects errors during @Test (no exception is thrown) and if you call org.testng.asserts.SoftAssert#assertAll at the end of @Testexception is thrown if there was any and test suite again continue with next @Test
If there are multiple assert in test case, and suppose your first assert fails, then in case of SoftAssert, test case will not fail immediately after assertion error has occurred. It continuous the processing for next statements. If will go till the end of the test even though multiple assertion error occurs. Finally we need to use assertAll(); method of SoftAssert, which will check if any of the assert exception has occurred, if any, then it will fail the test case (but after executing all the statements in the test).
Note: make sure that you use assertAll(); method at last. If you don’t use it, your test case will pass even if multiple assert errors are in test.
Below example shows the difference between softAssert & Hard Assert
Example –
package samplePackage;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class Asserts {
@Test
public void softAssertTest(){
System.out.println("------------OutPut for SoftAssert --------------------------");
//In case of SoftAssert, we need to create object of it, & then use that object (unlike normal assert)
SoftAssert a = new SoftAssert();
System.out.println("I am Before SoftAssert statment");
//Failing assert so that assertion error should occur
a.assertTrue(false);
System.out.println("I am After SoftAssert statment1");
a.assertTrue(false);
System.out.println("I am After SoftAssert statment2");
a.assertTrue(false);
System.out.println("I am After SoftAssert statment3");
a.assertAll();
}
}
Observe the below console output,
Even if assertion error occurs, it continued execution till end, & lastly it failed test.
Console Output:
[TestNG] Running:
C:UsersPrakashAppDataLocalTemptestng-eclipse–212981613testng-customsuite.xml
————OutPut for SoftAssert ————————–
I am Before SoftAssert statment
I am After SoftAssert statment1
I am After SoftAssert statment2
I am After SoftAssert statment3
FAILED: softAssertTest
java.lang.AssertionError: The following asserts failed:
expected [true] but found [false],
expected [true] but found [false],
expected [true] but found [false]
at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:48)
at samplePackage.Asserts.softAssertTest(Asserts.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:773)
at org.testng.TestRunner.run(TestRunner.java:623)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
Hope this helps !!!!