Java assertions underused

assertions are, in theory, for testing invariants, assumptions that must be true in order for the code to complete properly.

The example shown is testing for valid input, which isn’t a typical usage for an assertion because it is, generally, user supplied.

Assertions aren’t generally used in production code because there is an overhead and it is assumed that situations where the invariants fail have been caught as coding errors during development and testing.

Your point about them coming “late” to java is also a reason why they aren’t more widely seen.

Also, unit testing frameworks allow for some of the need for programmatic assertions to be external to the code being tested.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)