Class Preconditions
Each method in this class throws a PreconditionViolationException
if the precondition is violated.
DISCLAIMER
These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidAssert that the suppliedpredicateistrue.static voidAssert that the suppliedpredicateistrue.static <T extends Collection<?>>
@Nullable TcontainsNoNullElements(@Nullable T collection, String message) Assert that the supplied collection contains nonullelements.static <T extends Collection<?>>
@Nullable TcontainsNoNullElements(@Nullable T collection, Supplier<String> messageSupplier) Assert that the supplied collection contains nonullelements.static <T> T @Nullable []containsNoNullElements(T @Nullable [] array, String message) Assert that the supplied array contains nonullelements.static <T> T @Nullable []containsNoNullElements(T @Nullable [] array, Supplier<String> messageSupplier) Assert that the supplied array contains nonullelements.static StringAssert that the suppliedStringis not blank.static StringAssert that the suppliedStringis not blank.static int[]Assert that the supplied array is neithernullnor empty.static <T extends Collection<?>>
TAssert that the suppliedCollectionis neithernullnor empty.static <T extends Collection<?>>
TAssert that the suppliedCollectionis neithernullnor empty.static <T> T[]Assert that the supplied array is neithernullnor empty.static <T> T[]Assert that the supplied array is neithernullnor empty.static <T> TAssert that the suppliedObjectis notnull.static <T> TAssert that the suppliedObjectis notnull.
-
Method Details
-
notNull
@Contract("null, _ -> fail; !null, _ -> param1") public static <T> T notNull(@Nullable T object, String message) throws PreconditionViolationException Assert that the suppliedObjectis notnull.- Parameters:
object- the object to checkmessage- precondition violation message- Returns:
- the supplied object as a convenience
- Throws:
PreconditionViolationException- if the supplied object isnull- See Also:
-
notNull
@Contract("null, _ -> fail; !null, _ -> param1") public static <T> T notNull(@Nullable T object, Supplier<String> messageSupplier) throws PreconditionViolationException Assert that the suppliedObjectis notnull.- Parameters:
object- the object to checkmessageSupplier- precondition violation message supplier- Returns:
- the supplied object as a convenience
- Throws:
PreconditionViolationException- if the supplied object isnull- See Also:
-
notEmpty
@Contract("null, _ -> fail") @API(status=INTERNAL, since="1.11") public static int[] notEmpty(int @Nullable [] array, String message) throws PreconditionViolationException Assert that the supplied array is neithernullnor empty.- Parameters:
array- the array to checkmessage- precondition violation message- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException- if the supplied array isnullor empty- Since:
- 1.9
- See Also:
-
notEmpty
@Contract("null, _ -> fail") public static <T> T[] notEmpty(T @Nullable [] array, String message) throws PreconditionViolationException Assert that the supplied array is neithernullnor empty.WARNING: this method does NOT check if the supplied array contains any
nullelements.- Parameters:
array- the array to checkmessage- precondition violation message- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException- if the supplied array isnullor empty- See Also:
-
notEmpty
@Contract("null, _ -> fail") public static <T> T[] notEmpty(T @Nullable [] array, Supplier<String> messageSupplier) throws PreconditionViolationException Assert that the supplied array is neithernullnor empty.WARNING: this method does NOT check if the supplied array contains any
nullelements.- Parameters:
array- the array to checkmessageSupplier- precondition violation message supplier- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException- if the supplied array isnullor empty- See Also:
-
notEmpty
@Contract("null, _ -> fail") public static <T extends Collection<?>> T notEmpty(@Nullable T collection, String message) throws PreconditionViolationException Assert that the suppliedCollectionis neithernullnor empty.WARNING: this method does NOT check if the supplied collection contains any
nullelements.- Parameters:
collection- the collection to checkmessage- precondition violation message- Returns:
- the supplied collection as a convenience
- Throws:
PreconditionViolationException- if the supplied collection isnullor empty- See Also:
-
notEmpty
@Contract("null, _ -> fail") public static <T extends Collection<?>> T notEmpty(@Nullable T collection, Supplier<String> messageSupplier) throws PreconditionViolationException Assert that the suppliedCollectionis neithernullnor empty.WARNING: this method does NOT check if the supplied collection contains any
nullelements.- Parameters:
collection- the collection to checkmessageSupplier- precondition violation message supplier- Returns:
- the supplied collection as a convenience
- Throws:
PreconditionViolationException- if the supplied collection isnullor empty- See Also:
-
containsNoNullElements
@Contract("null, _ -> null") public static <T> T @Nullable [] containsNoNullElements(T @Nullable [] array, String message) throws PreconditionViolationException Assert that the supplied array contains nonullelements.WARNING: this method does NOT check if the supplied array is
nullor empty.- Parameters:
array- the array to checkmessage- precondition violation message- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException- if the supplied array contains anynullelements- See Also:
-
containsNoNullElements
@Contract("null, _ -> null") public static <T> T @Nullable [] containsNoNullElements(T @Nullable [] array, Supplier<String> messageSupplier) throws PreconditionViolationException Assert that the supplied array contains nonullelements.WARNING: this method does NOT check if the supplied array is
nullor empty.- Parameters:
array- the array to checkmessageSupplier- precondition violation message supplier- Returns:
- the supplied array as a convenience
- Throws:
PreconditionViolationException- if the supplied array contains anynullelements- See Also:
-
containsNoNullElements
@Contract("null, _ -> null") public static <T extends Collection<?>> @Nullable T containsNoNullElements(@Nullable T collection, String message) throws PreconditionViolationException Assert that the supplied collection contains nonullelements.WARNING: this method does NOT check if the supplied collection is
nullor empty.- Parameters:
collection- the collection to checkmessage- precondition violation message- Returns:
- the supplied collection as a convenience
- Throws:
PreconditionViolationException- if the supplied collection contains anynullelements- See Also:
-
containsNoNullElements
@Contract("null, _ -> null") public static <T extends Collection<?>> @Nullable T containsNoNullElements(@Nullable T collection, Supplier<String> messageSupplier) throws PreconditionViolationException Assert that the supplied collection contains nonullelements.WARNING: this method does NOT check if the supplied collection is
nullor empty.- Parameters:
collection- the collection to checkmessageSupplier- precondition violation message supplier- Returns:
- the supplied collection as a convenience
- Throws:
PreconditionViolationException- if the supplied collection contains anynullelements- See Also:
-
notBlank
@Contract("null, _ -> fail") public static String notBlank(@Nullable String str, String message) throws PreconditionViolationException Assert that the suppliedStringis not blank.A
Stringis blank if it isnullor consists only of whitespace characters.- Parameters:
str- the string to checkmessage- precondition violation message- Returns:
- the supplied string as a convenience
- Throws:
PreconditionViolationException- if the supplied string is blank- See Also:
-
notBlank
@Contract("null, _ -> fail") public static String notBlank(@Nullable String str, Supplier<String> messageSupplier) throws PreconditionViolationException Assert that the suppliedStringis not blank.A
Stringis blank if it isnullor consists only of whitespace characters.- Parameters:
str- the string to checkmessageSupplier- precondition violation message supplier- Returns:
- the supplied string as a convenience
- Throws:
PreconditionViolationException- if the supplied string is blank- See Also:
-
condition
@Contract("false, _ -> fail") public static void condition(boolean predicate, String message) throws PreconditionViolationException Assert that the suppliedpredicateistrue.- Parameters:
predicate- the predicate to checkmessage- precondition violation message- Throws:
PreconditionViolationException- if the predicate isfalse- See Also:
-
condition
@Contract("false, _ -> fail") public static void condition(boolean predicate, Supplier<String> messageSupplier) throws PreconditionViolationException Assert that the suppliedpredicateistrue.- Parameters:
predicate- the predicate to checkmessageSupplier- precondition violation message supplier- Throws:
PreconditionViolationException- if the predicate isfalse
-