{"id":16513958,"url":"https://github.com/twin/super-assert","last_synced_at":"2026-05-08T12:52:07.578Z","repository":{"id":57741037,"uuid":"146036703","full_name":"TwiN/super-assert","owner":"TwiN","description":"A complete and simple one-line assertion solution with support for custom exceptions in Java","archived":false,"fork":false,"pushed_at":"2019-02-01T21:09:31.000Z","size":102,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-12T23:12:03.429Z","etag":null,"topics":["assert","assertion-library","assertions","java","maven"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TwiN.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-24T20:26:30.000Z","updated_at":"2019-03-09T18:45:23.000Z","dependencies_parsed_at":"2022-09-06T23:31:04.835Z","dependency_job_id":null,"html_url":"https://github.com/TwiN/super-assert","commit_stats":null,"previous_names":["twinproduction/super-assert"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TwiN%2Fsuper-assert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TwiN%2Fsuper-assert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TwiN%2Fsuper-assert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TwiN%2Fsuper-assert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TwiN","download_url":"https://codeload.github.com/TwiN/super-assert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241494184,"owners_count":19971871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assert","assertion-library","assertions","java","maven"],"created_at":"2024-10-11T16:10:58.783Z","updated_at":"2026-05-08T12:52:07.539Z","avatar_url":"https://github.com/TwiN.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# super-assert\n\n[![Build Status](https://travis-ci.org/TwinProduction/super-assert.svg?branch=master)](https://travis-ci.org/TwinProduction/super-assert) [![Coverage Status](https://coveralls.io/repos/github/TwinProduction/super-assert/badge.svg?branch=master)](https://coveralls.io/github/TwinProduction/super-assert?branch=master)\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eorg.twinnation\u003c/groupId\u003e\n  \u003cartifactId\u003esuper-assert\u003c/artifactId\u003e\n  \u003cversion\u003e1.0.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nHeavily inspired by Spring Framework's \n[Assert](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/Assert.html) \nclass in `org.springframework.util` and JUnit's assertion library, SuperAssert's goal is to provide a complete\n one-line assertion solution with custom exception support. _This library is not meant to be used for testing._\n\nBy providing a one-line solution, developers significantly enhance the readability of their code by reducing \nthe amount of lines taken by minor, but sometimes numerous checks like this:\n\n```java\nif (username == null || username.length == 0) {\n\tthrow new Exception(\"...\");\n}\n```\n\nand instead, use an elegant one-line solution like this:\n\n```java\nSuperAssert.isTrue(username == null || username.length == 0, \"...\");\n```\n\nIn fact, SuperAssert shines even more when you want to give more details to each specific error cases:\n\n```java\nSuperAssert.notNull(username, \"Your username cannot be null\");\nSuperAssert.isFalse(username.isEmpty(), \"Your username cannot be blank\");\nSuperAssert.isAlphanumeric(username, \"Your username must contain only alphanumeric characters\");\nSuperAssert.isNull(getUserByUsername(username), \"The username you selected is already taken by another user\");\nSuperAssert.isAscii(password, \"Your password cannot use illegal characters\");\n```\n\nAs this project is currently in progress, you're welcome to contribute.\n\n## Table of Contents\n\n* [Usage](#usage)\n  + [Reading it properly](#reading-it-properly)\n  + [Without custom exception](#without-custom-exception)\n  + [With custom exception](#with-custom-exception)\n* [Real world examples](#real-world-examples)\n* [Current API](#current-api)\n  + [notNull](#notnull)\n  + [isNull](#isnull)\n  + [isTrue](#istrue)\n  + [isFalse](#isfalse)\n  + [notEmpty](#notempty)\n  + [notEmptyOrNull](#notemptyornull)\n  + [isAscii](#isascii)\n  + [isAlphanumeric](#isalphanumeric)\n  + [hasLength](#haslength)\n* [TODO](#todo)\n\n\n## Usage\n\nBy default, SuperAssert will throw an `IllegalArgumentException` with the message passed as parameter.\nIt's very important to understand that `IllegalArgumentException` is an **unchecked exception**, meaning that \nusing SuperAssert does not require you to declare an exception in the method's or in the constructor's `throws` clause\n**unless you specify a custom checked exception**.\n\n### Reading it properly\n\n- The name of the method is what is expected.\n- The first parameter is what is being asserted.\n- The last parameter is what will be thrown if the assertion fails.\n\ne.g. `SuperAssert.isTrue(condition, messageOrException);`\n\nThe above would translate to:\n\u003e I'm expecting that `condition` `isTrue` and if it is **not**, then `messageOrException` will be thrown.\n\n\n### Without custom exception\n\n```java\nSuperAssert.notNull(object, \"The object passed as parameter cannot be null\");\n```\n\n\n### With custom exception\n\nUsing custom exceptions involves passing the exception message as parameter to your custom exception and\nthen passing that custom exception as parameter instead of the message in the SuperAssert method.\n\n```java\nSuperAssert.notNull(object, new CustomException(\"The object passed as parameter cannot be null\"));\n```\n\nGoing from using the default exception chosen by SuperAssert to a custom exception:\n\n```java\nSuperAssert.notNull(user, \"User cannot be null\"); \n```\ntranslates to \n```java\nSuperAssert.notNull(user, new CustomException(\"User cannot be null\")); \n```\n\n\n\n----------------------\n\n## Real world examples\n\n```java\npublic User getUserById(Long id) {\n\tSuperAssert.notNull(id, \"A user cannot have a null id.\");\n\treturn userRepository.findById(id).orElse(null);\n}\n\n\npublic User createUser(String username, String password) {\n\tSuperAssert.notNull(username, \"Username cannot be null\");\n\tSuperAssert.notNull(password, \"Password cannot be null\");\n\tSuperAssert.isAlphanumeric(username, \"Username can only contain the following: a-z A-Z 0-9\");\n\tSuperAssert.isAscii(password, \"Password contains invalid character\");\n\tSuperAssert.isNull(getUserByUsername(username), \"Username is already taken\");\n\treturn userRepository.save(new User(username, hash(password)));\n}\n```\n\n----------------------\n\n\n## Current API\n\n\n### notNull\n\n**Syntax**: `SuperAssert.notNull(object, message | exception)`\n\nAsserts whether an object is not null. \n- If it is null, an exception will be thrown\n- If it isn't null, `true` will be returned \n\n#### Example\n\nThis will throw an exception, because the assertion that the `user` is `notNull` is **wrong** _(the user is null)_:\n```java\nUser user = null;\nSuperAssert.notNull(user, \"User cannot be null\"); \n```\nThis will return `true`, because the assertion that the `user` is `notNull` is **correct** _(the user is not null)_:\n```java\nUser user = new User();\nSuperAssert.notNull(user, \"User cannot be null\"); \n```\n\n\n### isNull\n\n**Syntax**: `SuperAssert.isNull(object, message | exception)`\n\nAsserts whether an object is null. \n- If it is not null, an exception will be thrown\n- If it is null, `true` will be returned \n\n#### Example\n\nThis will throw an exception, because the assertion that the `user` `isNull` is **wrong** _(the user is not null)_:\n```java\nUser user = new User();\nSuperAssert.notNull(user, \"User already exists\"); \n```\nThis will return `true`, because the assertion that the `user` `isNull` is **correct** _(the user is null)_:\n```java\nUser user = null;\nSuperAssert.notNull(user, \"User already exists\"); \n```\n\n\n### isTrue\n\n**Syntax**: `SuperAssert.isTrue(object, message | exception)`\n\nAsserts whether a boolean is true. \n- If it is `true`, `true` will be returned\n- If it is `false`, an exception will be thrown\n\n#### Example\n\nThis will throw an exception, because the assertion that the `title.isEmpty()` condition `isTrue` is **wrong** _(the title is not empty)_:\n```java\nString title = \"Some pretty cool title\";\nSuperAssert.isTrue(title.isEmpty(), \"Title cannot be empty\"); \n```\nThis will return `true`, because the assertion that the `title.isEmpty()` condition `isTrue` is **correct** _(the title is empty)_:\n```java\nString title = \"\";\nSuperAssert.isTrue(title.isEmpty(), \"Title cannot be empty\"); \n```\n\n\n### isFalse\n\n**Syntax**: `SuperAssert.isFalse(object, message | exception)`\n\nAsserts whether a boolean is false. \n- If it is `true`, an exception will be thrown\n- If it is `false`, `true` will be returned\n\n#### Example\n\nThis will throw an exception, because the assertion that the `locked` condition `isFalse` is **wrong**:\n```java\nboolean locked = true;\nSuperAssert.isFalse(closed, \"The door is locked, you can't get in!\"); \n```\nThis will return `true`, because the assertion that the `locked` condition `isFalse` is **correct**:\n```java\nboolean locked = false;\nSuperAssert.isFalse(closed, \"The door is locked, you can't get in!\"); \n```\n\n\n### notEmpty\n\n**Syntax**: `SuperAssert.notEmpty(object, message | exception)`\n\nAsserts whether a list is not empty. \n- If it is empty, an exception will be thrown\n- If it is not empty, `true` will be returned\n\n#### Example\n\nThis will throw an exception, because the assertion that the list of `users` is `notEmpty` is **wrong**:\n```java\nList\u003cUser\u003e users = new ArrayList\u003c\u003e();\nSuperAssert.notEmpty(users, \"The user list cannot be empty!\"); \n```\nThis will return `true`, because the assertion that the list of `users` is `notEmpty` is **correct**:\n```java\nList\u003cUser\u003e users = Arrays.asList(user1, user2, ...);\nSuperAssert.notEmpty(users, \"The user list cannot be empty!\"); \n```\n\n\n### notEmptyOrNull\n\n**Syntax**: `SuperAssert.notEmptyOrNull(object, message | exception)`\n\nAsserts whether a list is not empty or null. \n- If it is empty or null, an exception will be thrown\n- If it is not empty or null, `true` will be returned\n\n_In essence, this method is just an aggregation of `notNull` and `notEmpty`._\n\n#### Example\n\nThis will throw an exception, because the assertion that the list of `users` is `notEmptyOrNull` is **wrong** _(the user list is null)_:\n```java\nList\u003cUser\u003e users = null;\nSuperAssert.notEmptyOrNull(users, \"The user list cannot be empty or null!\"); \n```\nThis will return `true`, because the assertion that the list of `users` is `notEmptyOrNull` is **correct**:\n```java\nList\u003cUser\u003e users = Arrays.asList(user1, user2, ...);\nSuperAssert.notEmptyOrNull(users, \"The user list cannot be empty or null!\"); \n```\n\n\n### isAscii\n\n**Syntax**: `SuperAssert.isAscii(string | char, message | exception)`\n\nAsserts whether a character/string is a _printable_ ASCII. \n- If it is not a printable ASCII character, an exception will be thrown\n- If it is, `true` will be returned\n\n**NOTE**: _While the name of this method is `isAscii`, be aware that this assumes **PRINTABLE** \nASCII characters. In other words, characters whose value is between 32 and 126 inclusively._\n\n#### Example\n\nThis will throw an exception, because the assertion that the string `message` `isAscii` is **wrong**:\n```java\nString message = \"Well,\\n this is pretty cool!\";\nSuperAssert.isAscii(message, \"Invalid characters\"); \n```\nThis will return `true`, because the assertion that the string `message` `isAscii` is **correct**:\n```java\nString message = \"Well, This is pretty cool!\";\nSuperAssert.isAscii(message, \"Invalid characters\");\n```\n\n\n### isAlphanumeric\n\n**Syntax**: `SuperAssert.isAlphanumeric(string | char, message | exception)`\n\nAsserts whether a character/string is alphanumeric. \n- If it is not alphanumeric, an exception will be thrown\n- If it is, `true` will be returned\n\n#### Example\n\nThis will throw an exception, because the assertion that the string `username` `isAlphanumeric` is **wrong**:\n```java\nString username = \"John Doe\";\nSuperAssert.isAlphanumeric(username, \"Username must be alphanumeric (a-z A-Z 0-9)\"); \n```\nThis will return `true`, because the assertion that the string `username` `isAlphanumeric` is **correct**:\n```java\nString username = \"J0hnD03\";\nSuperAssert.isAlphanumeric(username, \"Username must be alphanumeric (a-z A-Z 0-9)\");\n```\n\n\n### hasLength\n\n**Syntax**: `SuperAssert.hasLength(string, minLength, [maxLength], message | exception)`\n\nAsserts whether a string respects the minimum and, if application, maximum length. \n- If it the length constraints are not respected, an exception will be thrown\n- If they are respected, `true` will be returned\n\nThe `maxLength` parameter is optional. If you do decide to use a maximum length, keep in mind that it\nhas to be higher than the `minLength`. Failure to do so will result in an `AssertionError`.\n\n#### Example\n\nThis will throw an exception, because the assertion that the string `password` `hasLength` with `minLength=8` and `maxLength=32` is **wrong**:\n```java\nString password = \"secret\";\nSuperAssert.hasLength(password, 8, 32, \"Your password must be between 8 and 32 characters inclusively\"); \n```\nThis will return `true`, because the assertion that the string `password` `hasLength` with `minLength=8` and `maxLength=32` is **correct**:\n```java\nString password = \"password123\";\nSuperAssert.hasLength(password, 8, 32, \"Your password must be between 8 and 32 characters inclusively\");\n```\n\n\n----------------------\n\n## TODO\n\n- Return nothing instead of `true`.\n\t- Update tests, documentation and comments accordingly\n\n\u003c!--- _The list is currently empty. If you have any idea, feel free to create an issue or a PR_ ---\u003e\n\t\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwin%2Fsuper-assert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwin%2Fsuper-assert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwin%2Fsuper-assert/lists"}