skip to content

In TestNG, how does a hard assert from org.testng.Assert differ from a SoftAssert, and when should you use each?

level: juniorimportance: must knowfreq 55%

answer

  1. stop at the first failure, or not
  2. AssertionError thrown on the spot
  3. failures recorded, verdict deferred
  4. assertAll() delivers the verdict

basics

~20 s

A hard assert from org.testng.Assert throws an AssertionError at the first failed check and ends the test method. A SoftAssert records each failure and keeps going; only its assertAll() call throws, listing every recorded failure.

solid answer

~40 s

The static methods of `org.testng.Assert` are **hard**: the first failing check throws `AssertionError`, the rest of the method never runs, and TestNG marks the test failed. `org.testng.asserts.SoftAssert` is an object you create: each `softly.assertEquals(...)` that fails is recorded instead of thrown, and nothing fails until you call `softly.assertAll()`, which throws one `AssertionError` listing every recorded failure. I use hard asserts for preconditions, because if the page or response did not load, every later check is noise, and soft asserts when one test verifies several independent fields, so a single run shows every mismatch. The classic trap is forgetting `assertAll()`: the failures are recorded but never thrown, and the test passes.

code

java · 24 lines
java
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class ProfileHeaderTest {

  record Profile(String name, String email, boolean verified) {}

  @Test
  public void headerShowsEveryField() {
    Profile profile = loadProfile("u-42");
    Assert.assertNotNull(profile, "profile loaded");

    SoftAssert softly = new SoftAssert();
    softly.assertEquals(profile.name(), "Ada Lovelace", "name");
    softly.assertEquals(profile.email(), "[email protected]", "email");
    softly.assertTrue(profile.verified(), "verified badge");
    softly.assertAll();
  }

  private Profile loadProfile(String id) {
    return new Profile("Ada Lovelace", "[email protected]", true);
  }
}

go deeper

for a junior

Recall that org.testng.Assert stops the method at the first failed check, while a SoftAssert records failures until assertAll() throws them together.

for a middle

Explain that SoftAssert is an instance you create per test method, that it intercepts only AssertionError from its own checks, and that assertAll() must be the last statement.

for a senior

Show judgment about mixing the styles: hard guards for preconditions, soft checks for independent fields, and the silent pass a missing assertAll() produces.

for a principal

Weigh whether a team should limit soft asserts, since a test checking a dozen things softly blurs what it specifies and spreads one root cause across many reported failures.

## Two assertion styles in TestNG TestNG ships two ways to check a result inside a test method. **Hard asserts** are the static methods of `org.testng.Assert`: `assertEquals`, `assertTrue`, `assertNotNull`, `fail` and the rest. **Soft asserts** come from `org.testng.asserts.SoftAssert`, a class you instantiate and call methods on. Both use the same argument order (the actual value first, the expected value second, an optional message last) and the same failure text, `expected [x] but found [y]`. What differs is *when* a failed check ends the test. ## How a hard assert behaves A hard assert throws `java.lang.AssertionError` the moment its check fails. Nothing after that line runs, and TestNG records the test method as **failed** with that error's message and stack trace. - The first failure hides every later one: if three fields are wrong, the report shows only the first, and you discover the other two on later runs. - That is exactly what you want for a **precondition**. If a page did not load or a response body is null, every later check would fail for the same root cause and bury it in noise. - Hard asserts keep no state between calls, so they behave the same whether the suite runs sequentially or in parallel. ## How SoftAssert behaves A `SoftAssert` is a **collector**. Its lifecycle has three steps: 1. Create it inside the test method: `SoftAssert softly = new SoftAssert();`. 2. Call its checks: `softly.assertEquals(...)`, `softly.assertTrue(...)`, even `softly.fail(...)`. Each check runs; if it fails, the `AssertionError` is caught and stored in the order the checks ran, and the method carries on. 3. Call `softly.assertAll()` as the last statement. If anything was recorded, it throws a single `AssertionError` whose message starts with `The following asserts failed:` (or your own header, through `assertAll(String)`) and lists every recorded failure. The verdict is therefore **deferred**: the test method fails only when `assertAll()` runs, and the report shows every mismatch from that one run. ## What the failure looks like For a profile test where the name and the verified badge are both wrong, `assertAll()` throws an error like this: ```text java.lang.AssertionError: The following asserts failed: name expected [Ada Lovelace] but found [Ada], verified badge expected [true] but found [false] ``` Each line carries the message you passed to that check, which is why a short label per soft check pays off: without it, a list of `expected [true] but found [false]` lines is hard to map back to the code. ## Choosing between them | Situation | Better choice | Why | |---|---|---| | The object under test must exist before anything else is checked | Hard `Assert` | Later checks are meaningless without it | | One test verifies several independent fields of one result | `SoftAssert` | One run reports every mismatch | | A failed check would make the next line throw | Hard `Assert` | Avoids a cascade of follow-on errors | | A multi-step flow where each step depends on the previous one | Hard `Assert` | A soft failure would let the flow continue in a broken state | A common, sound pattern combines them: hard guards first, then a block of soft checks on independent properties, then `assertAll()` as the final line. ## The mistakes interviewers listen for - **Forgetting `assertAll()`.** TestNG does not call it for you. The failures are recorded and never thrown, the method returns normally, and the test **passes**. This is the most damaging soft-assert bug because nothing in the report hints at it. - **Expecting SoftAssert to catch exceptions.** It intercepts only the `AssertionError` raised by its own check methods. A `NullPointerException` from the code under test, or a hard `Assert` call, ends the method at once; `assertAll()` never runs, so the soft failures recorded so far are not reported. - **Making every check soft.** A test that continues past a broken precondition turns one root cause into a dozen reported failures and makes triage slower, not faster. - **Reusing one instance across test methods.** A `SoftAssert` never clears what it recorded, so an instance shared between methods carries one method's failures into the next. Create a fresh one in each test method. ## Contrast with JUnit Jupiter JUnit Jupiter has no collector object: it groups checks by passing lambdas to `Assertions.assertAll`, and its `assertEquals` takes the expected value first. Those mechanics belong to JUnit. In TestNG, the deferred verdict lives in the `SoftAssert` instance you create and in the `assertAll()` call you must not forget.

  • What happens if code between two soft checks throws a NullPointerException?
    The exception propagates immediately, exactly as it would without SoftAssert, because SoftAssert only catches the AssertionError raised by its own check methods. assertAll() is never reached, so soft failures recorded earlier are not reported; the test fails with only the NullPointerException. Put hard guards before the soft block so a missing object stops the test cleanly.
  • Does calling fail("...") on a SoftAssert end the test method?
    No. On a SoftAssert, fail() goes through the same record-and-continue path as every other check, so the failure is stored and the method keeps running; it surfaces only when assertAll() throws. The static Assert.fail() is the one that throws at once.
  • What does the message thrown by SoftAssert.assertAll() contain?
    By default it starts with 'The following asserts failed:' and then lists each recorded failure on its own line, in the order the checks ran, each with the label you passed to that check, such as 'email expected [[email protected]] but found [ada@test]'. assertAll(String) replaces the header with your own text.

A soft assert is like a building inspector filling in a defect checklist while walking every room: nothing is rejected until the checklist is handed in, and a checklist that is never handed in rejects nothing.

  • A SoftAssert failure fails the test as soon as that check runs.
  • TestNG calls assertAll() automatically when the test method returns.
  • SoftAssert also catches exceptions thrown by the code under test.
  • Soft asserts should replace hard asserts everywhere so each run shows more failures.
  • TestNG's Assert and SoftAssert take the expected value first, like JUnit Jupiter.