SoftAssert in TestNG example

SoftAssert in TestNG helps to collect all the assertions throughout the @Test method. And to see assertions result at the end of the test, we have to invoke assertAll().

SoftAssert don't throw an exception when an assert fails. The test execution will continue with the next step after the assert statement.

If we use normal asserts like Assert.assertTrue() or Assert.assertEquals() in TestNG, @Test Method will immediately fail after any of the Asserts fails.

There are multiple scenarios where you want to continue the execution even if some assert fails and see the result at the end of the test. For instance, after landing to a page where you want to validate multiple cases we can use softAsserts and throw error at the end of the test execution / before user navigates to next page.

Let us see a simple example on TestNG SoftAssert and compare Hard Asserts:-

package com.example;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class TestOne {

	@Test
	public void testCaseOne() {
		System.out.println("*** test case one started ***");
		Assert.assertEquals(5, 5, "First hard assert failed");
		System.out.println("hard assert success....");
		Assert.assertTrue("Hello".equals("hello"), "Second hard assert failed");
		System.out.println("*** test case one executed successfully ***");
	}

	@Test
	public void testCasetwo() {
		SoftAssert softAssert = new SoftAssert();
		System.out.println("*** test case two started ***");
		softAssert.assertEquals("Hello", "Hello", "First soft assert failed");
		System.out.println("hard assert success....");
		softAssert.assertTrue("Hello".equals("hello"), "Second soft assert failed");
		softAssert.assertTrue("Welcome".equals("welcomeeee"), "Third soft assert failed");
		System.out.println("*** test case two executed successfully ***");
		softAssert.assertAll();
	}

}

Once you execute the above code, you should see output something like below : -

[RemoteTestNG] detected TestNG version 6.14.3
*** test case one started ***
hard assert success....
*** test case two started ***
hard assert success....
*** test case two executed successfully ***
FAILED: testCaseOne
java.lang.AssertionError: Second hard assert failed expected [true] but found [false]

1. testCaseOne will fail at below step and test will fail immediately.

Assert.assertTrue("Hello".equals("hello"), "Second hard assert failed");

2. testCasetwo Will also fail at below step, but will continue and execute other assertions until the last step softAssert.assertAll();

 softAssert.assertTrue("Hello".equals("hello"), "Second soft assert failed");

Points to remember : -
1. We should instantiate a SoftAssert object within a @Test method. Scope of SoftAssert should only be within the Test method as seen the above example.
2. We should never use the same Soft Assertions with multiple test cases. In the below example, we are using the same object of SoftAssert class with multiple test cases and see the result which includes multiple test cases.

package com.example;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class TestOne {
	
	SoftAssert softAssert = new SoftAssert();

	@Test
	public void testCasetwo() {
	
		System.out.println("*** test case two started ***");
		softAssert.assertEquals("Hello", "Hello", "First soft assert failed - testCasetwo");
		System.out.println("Soft assert success....");
		softAssert.assertTrue("Hello".equals("hello"), "Second soft assert failed - testCasetwo");
		softAssert.assertTrue("Welcome".equals("welcomeeee"), "Third soft assert failed - testCasetwo");
		System.out.println("*** test case two executed successfully ***");
		softAssert.assertAll();
	}
	
	@Test
	public void testCaseThree() {
		
		System.out.println("*** test case three started ***");
		softAssert.assertEquals("Hello", "Hello", "First soft assert failed - testCaseThree");
		System.out.println("Soft assert success....");
		softAssert.assertTrue("Hello".equals("hello"), "Second soft assert failed - testCaseThree");
		softAssert.assertTrue("Welcome".equals("welcomeeee"), "Third soft assert failed - testCaseThree");
		System.out.println("*** test case three executed successfully ***");
		softAssert.assertAll();
	}

}

The results for one test methods looks something like below : -

FAILED: testCaseThree
java.lang.AssertionError: The following asserts failed:
	Second soft assert failed - testCaseThree expected [true] but found [false],
	Third soft assert failed - testCaseThree expected [true] but found [false]
	at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
	at com.example.TestOne.testCaseThree(TestOne.java:31)

And the output for other second test method looks like below :-

FAILED: testCasetwo
java.lang.AssertionError: The following asserts failed:
	Second soft assert failed - testCaseThree expected [true] but found [false],
	Third soft assert failed - testCaseThree expected [true] but found [false],
	Second soft assert failed - testCasetwo expected [true] but found [false],
	Third soft assert failed - testCasetwo expected [true] but found [false]
	at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
	at com.example.TestOne.testCasetwo(TestOne.java:19)

if you observe the above output, testCasetwo Test method also shows asserts errors of other test method testCaseThree

image: 
testng soft assert example
Test Frameworks: 

Add new comment