Ad Space — Top Banner

AssertionError

Java Programming Language

Severity: Moderate

What Does This Error Mean?

AssertionError is thrown when a Java assert statement fails. An assert statement is a sanity check you can place in your code — it says 'I am certain this condition is always true here. If it is not, something is very wrong.' If the condition turns out to be false, Java throws AssertionError to stop the program immediately. Assertions must be enabled with the -ea JVM flag — they are disabled by default.

Affected Models

  • Java 1.4 and later
  • Java SE
  • Java EE
  • Android (limited)

Common Causes

  • An assert statement found a condition that should always be true but is actually false
  • A programming assumption turned out to be wrong — the code is in an unexpected state
  • Running JUnit or TestNG tests where the test assertion failed (test frameworks throw AssertionError when tests fail)
  • An assert in a library you are using detected an invalid internal state
  • Using assert to validate method parameters (considered bad practice — use exceptions for that instead)

How to Fix It

  1. Read the assertion error message. It will tell you which assert failed and sometimes what the unexpected value was.

    The full stack trace shows which file and line the assert is on. Go to that line in the code.

  2. Understand what the assert is checking. The assertion represents a developer's promise about what should be true at that point in the code. Why is it false?

    Add logging or a debugger to inspect the values involved. The bug is in the logic that led to the unexpected state, not in the assert itself.

  3. If this is a JUnit or TestNG test failure, the AssertionError means a test assertion failed — like assertEquals(expected, actual) where expected and actual do not match.

    Fix the underlying code being tested, or update the test if the expected behavior has legitimately changed.

  4. To run a Java program with assertions enabled, pass the -ea flag to the JVM: java -ea MyProgram — assertions are disabled by default in Java.

    Most IDEs have a setting to enable assertions during development. In IntelliJ: Run/Debug Configurations > VM options > add -ea

  5. Do NOT use assert statements to validate method arguments in production code. Use proper exceptions like IllegalArgumentException instead. Assertions are for development-time sanity checks, not production validation.

    Because assertions are disabled by default, any validation using assert will silently skip in production — giving no protection at all.

When to Call a Professional

An AssertionError means the program reached a state that should be impossible according to the programmer's assumptions. This is a real bug — investigate why the assumption was wrong and fix the logic that violated it. Do not just remove the assert statement — that would hide the bug without fixing it.

Frequently Asked Questions

Are Java assertions enabled by default?

No — Java assertions are disabled by default. You must explicitly enable them with the -ea (enable assertions) JVM flag when running your program. Without -ea, all assert statements are silently ignored at runtime. This is why assert should not be used for production validation — it does nothing unless explicitly turned on.

What is the difference between AssertionError in plain Java and in JUnit?

In plain Java, AssertionError is thrown by the assert keyword when its condition is false. In JUnit/TestNG, AssertionError is thrown by assertion methods like assertEquals(), assertTrue(), etc. when the check fails. Both use the same AssertionError class — the difference is how it is thrown. JUnit's assertion failures show which expected and actual values differed.

Should I use assert for input validation?

No — use proper exceptions for input validation. For method arguments, throw IllegalArgumentException. For invalid object state, throw IllegalStateException. Assert is for internal consistency checks during development — things that should never happen if the code is correct. In production, use if-then-throw patterns, not assert.