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
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> void
assertEquals
(T expected, T actual) static void
assertFalse
(boolean condition) Abort program execution ifcondition
is true.static void
assertFalse
(boolean condition, String errorMessage) static void
assertFalse
(boolean condition, Supplier<String> errorMessage) static void
Abort program execution ifo
isnull
.static void
assertNull
(Object o) Abort program execution ifo
is notnull
.static void
assertTrue
(boolean cond) Abort program execution ifcond
is false.static void
assertTrue
(boolean cond, String errorMessage) Abort program execution ifcond
is false.static void
assertTrue
(boolean cond, Supplier<String> errorMessage) Abort program execution ifcond
is false.static void
ThrowsAssertionError
witherrorMessage
as error message.
-
Constructor Details
-
Assert
public Assert()
-
-
Method Details
-
assertTrue
public static void assertTrue(boolean cond) Abort program execution ifcond
is 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
- ifcond
is false
-
assertTrue
Abort program execution ifcond
is 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
- ifcond
is false
-
assertTrue
Abort program execution ifcond
is false.If
errorMessage
is 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
- ifcond
is false
-
assertFalse
public static void assertFalse(boolean condition) Abort program execution ifcondition
is 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
- ifcondition
is true
-
assertFalse
-
assertFalse
-
fail
ThrowsAssertionError
witherrorMessage
as error message. -
assertNotNull
Abort program execution ifo
isnull
. -
assertNull
Abort program execution ifo
is notnull
. -
assertEquals
public static <T> void assertEquals(T expected, T actual)
-