Assertions functionality for Android Applications.
Early discovery of application issues. See the full reasoning here.
Add dependency from jcenter
:
implementation 'com.heershingenmosiken:assertions-android:1.+'
Initialize in your Application::onCreate(...) method.
// Application should crash on assertion only in debug mode
AndroidAssertions.shouldCrashOnAssertion(BuildConfig.DEBUG);
// In any case we would like to report raised assertion to crashlytics as Non-Fatal exception
AndroidAssertions.addAssertionHandler(assertionData -> Crashlytics.logException(assertion.throwable));
We have also gradle library for pure Java modules, see below.
API when assertion state occurred.
AndroidAssertions.fail(new IllegalStateException("Unreachable code"));
API for variables state validation, see ExceptionFactory concept, explanation.
AndroidAssertions.assertTrue(shouldBeTrue, () -> new IllegalStateException("Value is not true"));
AndroidAssertions.assertEmpty(collection, () -> new IllegalStateException("Collection is not empty"));
AndroidAssertions.assertNotNull(object, () -> new IllegalStateException("Collection is not empty"));
Hint 1: Meaningful messages are important and help faster understand what happens without opening the editor. Hint 2: You can add information application state to Exceptions message.
If you want to add an assertion to a pure java module(no android) and do not want to bring android dependency there, we have a pure assertions-java
java library.
It is a core part of the assertion library and assertions-android
depends on it, so you may add it as follows:
implementation 'com.heershingenmosiken:assertions-java:1.+'
It will share the same AssertionHandlers with assertions-android
module.
We have no third-party dependencies. You may reuse it as source code.
There are situations when Assertion happens because of dependency that is out of your control and it is not possible to fix it right now.
In this case, you may use failSilently
method.
AndroidAssertions.failSilently(new IllegalStateException("Please do not use failSilently to often."));
It will trigger AssertionHandler, but will not crash the application.
- Java assertions
- Guava preconditions
- Dart assertions
- Lots of others
Feel free to make PRs and Raise issues or Feature Requests if you will have any.
Stanislav Dekalo @dekalo-stanislav