Class Assert
Assertions should document preconditions and postconditions which aren't enforced by the type
system. All assertions in this class throw AssertionError to abort the program
execution. At no point an AssertionError should be catched and the normal control flow
resumed. The only valid reasons to catch such an exception is to add additional information to
the exception or to ensure a fast and correct program shutdown.
The default Java assertions are disabled by default which makes them unsuitable for this
research project because correctness is a higher goal then a little performance. This class
provides assertions in a similar manner as JUnit and should be used for all assertions outside
of unit tests (for unit tests use JUnit's assertions). Disabling or Enabling Java's
assert should make no difference whatsoever.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidassertEquals(T expected, T actual) static voidassertFalse(boolean condition) Abort program execution ifconditionis true.static voidassertFalse(boolean condition, String errorMessage) static voidassertFalse(boolean condition, Supplier<String> errorMessage) static voidAbort program execution ifoisnull.static voidassertNull(Object o) Abort program execution ifois notnull.static voidassertTrue(boolean cond) Abort program execution ifcondis false.static voidassertTrue(boolean cond, String errorMessage) Abort program execution ifcondis false.static voidassertTrue(boolean cond, Supplier<String> errorMessage) Abort program execution ifcondis false.static voidThrowsAssertionErrorwitherrorMessageas error message.
-
Constructor Details
-
Assert
public Assert()
-
-
Method Details
-
assertTrue
public static void assertTrue(boolean cond) Abort program execution ifcondis false.If the checked condition is not obvious in the source code
assertTrue(boolean,java.lang.String)should be used to help identifying issues quickly.- Parameters:
cond- the condition which has to be true- Throws:
AssertionError- ifcondis false
-
assertTrue
Abort program execution ifcondis false.Overload of
assertTrue(boolean,java.lang.String)for computationally expensive messages. It's used to save the little execution time to construct a helpful error message in the common case of a correct assumption.- Parameters:
cond- the condition which has to be trueerrorMessage- a supplier of a single message identifying what condition is checked- Throws:
AssertionError- ifcondis false
-
assertTrue
Abort program execution ifcondis false.If
errorMessageis computationally expensive, consider usingassertTrue(boolean,java.util.function.Supplier<java.lang.String>).- Parameters:
cond- the condition which has to be trueerrorMessage- a message identifying what condition is checked- Throws:
AssertionError- ifcondis false
-
assertFalse
public static void assertFalse(boolean condition) Abort program execution ifconditionis true.If the checked condition is not obvious in the source code
assertTrue(boolean,java.lang.String)should be used to help identifying issues quickly.- Parameters:
condition- the condition which has to be false- Throws:
AssertionError- ifconditionis true
-
assertFalse
-
assertFalse
-
fail
ThrowsAssertionErrorwitherrorMessageas error message. -
assertNotNull
Abort program execution ifoisnull. -
assertNull
Abort program execution ifois notnull. -
assertEquals
public static <T> void assertEquals(T expected, T actual)
-