The Java assert keyword checks a condition the programmer believes is always true and stops the program with an AssertionError if it isn't. It's a tool for catching bugs during development, and it's easy to confuse with the assertions testers use every day in TestNG, JUnit and AssertJ. This guide explains both, and when to use each.
The assert Statement
assert condition;
assert condition : "message shown when the condition is false";
int discount = calculateDiscount(cart);
assert discount >= 0 && discount <= 100 : "discount out of range: " + discount;
Assertions Are Off by Default
The JVM ignores assert statements unless you enable them, so they cost nothing in production:
java -ea com.example.App # enable for all your classes
java -ea:com.example... com.example.App # enable for one package and its sub-packages
java -da com.example.App # disable (the default)
In IntelliJ or Eclipse, add -ea to the VM options of the run configuration. Maven Surefire enables assertions by default when it runs tests.
When to Use assert, and When Not To
| Use assert for | Don't use assert for |
|---|---|
| Internal conditions that should be impossible if your code is correct | Validating arguments of public methods; throw IllegalArgumentException instead |
| Checking private helper logic and invariants while developing | Checking user input, files or API responses, which can really be wrong |
| Documenting assumptions in code | Anything with side effects: assert list.remove(x); does nothing when assertions are off |
The last row is the classic trap: because assertions can be disabled, the expression inside must never do real work.
Java assert vs Test Assertions
In test code you almost never use the assert keyword. Test frameworks provide assertion methods that always run, produce clear failure messages and integrate with reports:
assert keyword | TestNG / JUnit / AssertJ | |
|---|---|---|
| Runs by default? | No, needs -ea | Yes, always |
| Purpose | Catch programming bugs | Verify the application's behaviour |
| Failure | AssertionError with your message | AssertionError with expected vs actual values |
| Reporting | None | Shown in test reports and CI |
// TestNG: assertEquals(actual, expected, message)
Assert.assertEquals(driver.getTitle(), "Swag Labs", "page title");
Assert.assertTrue(cartBadge.isDisplayed(), "cart badge should be visible");
// JUnit 5: assertEquals(expected, actual) — note the reversed order
Assertions.assertEquals("Swag Labs", driver.getTitle());
// AssertJ: fluent and readable, works with any runner
assertThat(productNames).hasSize(6).contains("Sauce Labs Backpack").isSorted();
Watch the argument order: TestNG takes (actual, expected), JUnit takes (expected, actual). Swapping them gives confusing failure messages.
Hard vs Soft Assertions
A normal (hard) assertion stops the test at the first failure. A soft assertion collects failures and reports them all at the end, which is useful when checking many fields on one page:
SoftAssert soft = new SoftAssert();
soft.assertEquals(profile.getName(), "Asha");
soft.assertEquals(profile.getEmail(), "asha@example.com");
soft.assertEquals(profile.getRole(), "Admin");
soft.assertAll(); // without this, failures are silently ignored
AssertJ offers the same with SoftAssertions.assertSoftly(...), and JUnit 5 with assertAll(...).
FAQs
Are assertions enabled by default in Java?
No. The JVM skips assert statements unless you start it with -ea (or -enableassertions). Maven Surefire turns them on by default when running tests.
What happens when a Java assertion fails?
The JVM throws an AssertionError with the optional message, which stops the current thread unless something catches it.
Should I use assert in Selenium tests?
No. Use the assertion methods of TestNG, JUnit or AssertJ: they always run, show expected and actual values, and appear in your reports.
What is the difference between assertEquals in TestNG and JUnit?
The argument order. TestNG's assertEquals(actual, expected) puts the actual value first; JUnit's assertEquals(expected, actual) puts the expected value first.