The ensurelib library
The ensurelib library is used to state conditions that must be verified in source code. It acts as a replacement for standard java assertions and Google's guava code.
The ensurelib library has the following advantages over java assertions and guava:
- It is always enabled. java assertions can be enabled or disabled but their default is disabled making them less useful. In many practical situations, it is difficult to change the default.
- It doesn't require conditions in source code, which generally lower code coverage. Consider, for example, the following code:
Many code coverage tools like jacoco will report that there are two branches in that statement: one for x > 0 and one for x <= 0. However, because it is an assertion, it may not even be possible to test the case in which x <= 0 and this leads to a false lower code coverage. Instead, using ensurelib, you write:
Ensure.greater(x, 0, "x <= 0");
This way, you get 100% code coverage with just one pass which is what one would expect.
- It allows for state conditions, pre-conditions, post-conditions, and a lot more because, unline guava, it doesn't assume what type of condition you are verifying. In fact, ensurelib allows you to place ensure statements anywhere in your code stating that you are sure that the condition is verified. As a consequence, ensurelib does not throw IllegalArgumentException or IllegalStateException but a rather more generic run-time exception, EnsureFailedException.