Quick revision
The short answer
The tests passed because the build tool ran them with assertions enabled: Maven Surefire and Gradle's test task both turn on -ea by default. The production JVM was started without -ea, so every assert statement was skipped and the broken invariants went through silently. Invariants that matter in production must be explicit checks that always run, throwing IllegalStateException or a domain exception with logging and metrics, backed by invariant or property-based tests and a clear decision about whether a violation fails the request or raises an alert.
What to remember
In this answer, we saw that the invariants were checked with assert, the build tool ran tests with assertions enabled by default, and the production JVM ran without -ea, so the checks were skipped and the violations passed silently. We replaced them with explicit checks that always run, throw IllegalStateException or a domain exception, and log and count every violation. We decided per step whether a violation fails the request or alerts and continues, added property-based invariant tests to find violations before release, and kept assert only for cheap internal checks whose silent skipping is acceptable.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The tests passed because the build tool ran them with assertions enabled: Maven Surefire and Gradle's test task both turn on -ea by default. The production JVM was started without -ea, so every assert statement was skipped and the broken invariants went through silently. Invariants that matter in production must be explicit checks that always run, throwing IllegalStateException or a domain exception with logging and metrics, backed by invariant or property-based tests and a clear decision about whether a violation fails the request or raises an alert.
Why this question is asked
This scenario is a real and expensive kind of bug: the safety net was there, the tests proved it worked, and still the money was wrong. Interviewers ask it to see whether we know how assertions are enabled, and, more importantly, how we protect rules that must never break in a payment system. In this answer, we will reconstruct what happened, see why the tests gave false confidence, replace the assertions with checks that always run, decide what a violation should do to the request, and add tests that find violations before release.
1Reconstructing What Happened
Suppose the developer wrote a fee calculation and guarded it with assertions: the fee is never negative, and the net amount plus the fee equals the gross amount.
In the test suite, a deliberately broken FeePlan made the first assertion fail, so the developer saw the check working. In production, a rounding change in a new FeePlan produced fees one cent too high for some currencies. No error was logged, no request failed, and the discrepancy was only found in a reconciliation report weeks later.
2Why the Tests Passed and Production Did Not
An assert statement only runs when the JVM is started with -ea. The test run had it; production did not. Maven Surefire's enableAssertions setting defaults to true, and Gradle's Test task also enables assertions by default, so every unit test executed the assertions. The production service was started with a plain java -jar, without -ea, so the JVM skipped every assert line as if it were not there.
So the tests were not wrong; they proved something about a configuration that production never used. This is the core lesson: an assert is a development-time check, and anything that must hold in production cannot depend on a JVM flag.
| Test run | Production | |
|---|---|---|
| JVM flags | -ea added by Surefire or Gradle | java -jar app.jar, no -ea |
| assert lines | Executed | Skipped entirely |
| Broken invariant | AssertionError, test fails | No error, wrong amount saved |
| What we learned | The check works | Nothing; the check never ran |
3Turning Critical Invariants Into Explicit Checks
For a payment calculation, the invariants are business rules, so they become ordinary code that always runs. We throw IllegalStateException, or better a domain exception such as SettlementInvariantViolation, because a broken invariant means our own state is wrong, not that the caller passed something bad. We include the values in the message, log them, and count them with a metric so dashboards and alerts can see them.
Notice that the check costs a comparison and an addition per settlement, which is nothing next to a database write. There is no performance argument for leaving it out.
4Fail the Request or Alert and Continue?
Once the check always runs, we must decide what a violation does. This is a business decision as much as a technical one, and we should make it explicitly with the people who own the money flow.
For a payment that has not yet been sent, failing is usually right: we reject the settlement, the transaction rolls back, and the customer can retry once we fix it. Sending a wrong amount is far more expensive to undo than a failed request. For a downstream step such as a report or a loyalty-points calculation, it may be better to continue, record the violation, and alert, because stopping would block thousands of correct operations over a cosmetic difference. Either way, the violation must be visible: a log line with the values, a metric, and an alert on any non-zero count.
- Check the invariant on every call
- Violated?YesRecord values, increment metricNoReturn the result
- Would continuing move wrong money?YesThrow and roll back the requestNoContinue, mark the record for review
- Alert on any violationa count above zero pages the owning team
4 steps in order. Play them, or step through them one at a time.
5Catching Violations Before Release
The bug escaped because no test exercised the new rounding with the currencies that broke it. Example-based tests check the cases we thought of; invariant tests check a rule across many generated inputs. A property-based testing library such as jqwik generates hundreds of amounts, currencies and fee plans and checks that net plus fee always equals gross.
A one-cent rounding error in one currency is exactly the kind of case such a test finds in seconds. We can add a reconciliation job in production as a last line of defence that compares totals daily, so even an unforeseen violation is found in a day rather than in weeks.
6Where assert Still Belongs
Assertions are not banned. They are fine for cheap internal sanity checks where silently skipping them in production is acceptable, such as checking that a private helper received a sorted list or that an internal cache index is in range. The test is simple: if this condition were false in production, would we want to know? If the answer is yes, it is an explicit check, not an assert.
7Follow-up questions
Why not just run production with -ea?
It would enable every assertion in our code and in every library that uses them, including expensive ones written on the assumption that they never run in production. It also leaves a business rule depending on a deployment flag that a future Dockerfile change could drop. An explicit check states the rule in the code itself.
Should the check throw IllegalStateException or a custom exception?
A custom subclass of IllegalStateException is a good middle ground. It signals that our own state is inconsistent, it can carry the gross, fee and net values as fields, and a global handler or alert rule can recognise it by type without parsing messages.
How do we make sure nobody adds assert for business rules again?
Write it into the team's coding guidelines and add a static analysis rule or a simple build check that flags assert statements in the payment packages. Code review then has a clear standard to point at.
8Conclusion
In this answer, we saw that the invariants were checked with assert, the build tool ran tests with assertions enabled by default, and the production JVM ran without -ea, so the checks were skipped and the violations passed silently. We replaced them with explicit checks that always run, throw IllegalStateException or a domain exception, and log and count every violation. We decided per step whether a violation fails the request or alerts and continues, added property-based invariant tests to find violations before release, and kept assert only for cheap internal checks whose silent skipping is acceptable.
Open with
The reason is how assertions are enabled. An assert statement only runs when the JVM has -ea. Maven Surefire and Gradle's test task enable assertions by default, so in tests the checks ran and even failed when they should.
Then, if they want more
The production service starts with plain java -jar, without -ea, so every assert was skipped and the broken invariant went through with no error at all. For invariants that matter in production, like a fee never being negative and net plus fee equalling gross, I would replace the asserts with explicit checks that always run.
On a violation I would log the values, increment a metric, and throw an IllegalStateException or a domain subclass of it, and I would alert on any non-zero count.
Then I would decide with the business owner what a violation should do: before money moves, fail and roll back the request, because a wrong payment is much harder to undo; in downstream steps like reporting, maybe continue, flag the record and alert.
To catch these before release I would add property-based tests, for example with jqwik, that check the invariant across many generated amounts and currencies, plus a daily reconciliation as a safety net. I would keep assert only for cheap internal sanity checks where silently skipping them is acceptable.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A developer adds assert statements to check invariants in a payment calculation and...": https://scrivio-interview-library.netlify.app/java/exceptions/a70abefe8a/