Top Description Constructors Methods
org.junit.platform.commons.util

public final Class Preconditions

extends Object
Class Inheritance
Annotations
@API
status:INTERNAL
since:1.0
Static Imports
org.apiguardian.api.API.Status.INTERNAL

Collection of utilities for asserting preconditions for method and constructor arguments.

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

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
condition(boolean
the predicate to check
predicate
,
String
precondition violation message
message
)

Assert that the supplied predicate is true.

public static void
condition(boolean
the predicate to check
predicate
,
Supplier<String>
precondition violation message supplier
messageSupplier
)

Assert that the supplied predicate is true.

public static <T> @Nullable T[]

Returns:

the supplied array as a convenience
containsNoNullElements
(@Nullable T[]
the array to check
array
,
String
precondition violation message
message
)

Assert that the supplied array contains no null elements.

public static <T> @Nullable T[]

Returns:

the supplied array as a convenience
containsNoNullElements
(@Nullable T[]
the array to check
array
,
Supplier<String>
precondition violation message supplier
messageSupplier
)

Assert that the supplied array contains no null elements.

public static <T extends Collection<?>> @Nullable T

Returns:

the supplied collection as a convenience
containsNoNullElements
(@Nullable T
the collection to check
collection
,
String
precondition violation message
message
)

Assert that the supplied collection contains no null elements.

public static <T extends Collection<?>> @Nullable T

Returns:

the supplied collection as a convenience
containsNoNullElements
(@Nullable T
the collection to check
collection
,
Supplier<String>
precondition violation message supplier
messageSupplier
)

Assert that the supplied collection contains no null elements.

public static String

Returns:

the supplied string as a convenience
notBlank
(@Nullable String
the string to check
str
,
String
precondition violation message
message
)

Assert that the supplied String is not blank.

public static String

Returns:

the supplied string as a convenience
notBlank
(@Nullable String
the string to check
str
,
Supplier<String>
precondition violation message supplier
messageSupplier
)

Assert that the supplied String is not blank.

public static int[]

Returns:

the supplied array as a convenience
notEmpty
(@Nullable int[]
the array to check
array
,
String
precondition violation message
message
)

Assert that the supplied array is neither null nor empty.

public static <T> T[]

Returns:

the supplied array as a convenience
notEmpty
(@Nullable T[]
the array to check
array
,
String
precondition violation message
message
)

Assert that the supplied array is neither null nor empty.

public static <T> T[]

Returns:

the supplied array as a convenience
notEmpty
(@Nullable T[]
the array to check
array
,
Supplier<String>
precondition violation message supplier
messageSupplier
)

Assert that the supplied array is neither null nor empty.

public static <T extends Collection<?>> T

Returns:

the supplied collection as a convenience
notEmpty
(@Nullable T
the collection to check
collection
,
String
precondition violation message
message
)

Assert that the supplied Collection is neither null nor empty.

public static <T extends Collection<?>> T

Returns:

the supplied collection as a convenience
notEmpty
(@Nullable T
the collection to check
collection
,
Supplier<String>
precondition violation message supplier
messageSupplier
)

Assert that the supplied Collection is neither null nor empty.

public static <T> T

Returns:

the supplied object as a convenience
notNull
(@Nullable T
the object to check
object
,
String
precondition violation message
message
)

Assert that the supplied Object is not null.

public static <T> T

Returns:

the supplied object as a convenience
notNull
(@Nullable T
the object to check
object
,
Supplier<String>
precondition violation message supplier
messageSupplier
)

Assert that the supplied Object is not null.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Preconditionsback to summary
private Preconditions()

Method Detail

conditionback to summary
public static void condition(boolean predicate, String message) throws PreconditionViolationException

Assert that the supplied predicate is true.

Parameters
predicate:boolean

the predicate to check

message:String

precondition violation message

Annotations
@Contract:false, _ -> fail
Exceptions
PreconditionViolationException:
if the predicate is false
See Also
condition(boolean, Supplier)
conditionback to summary
public static void condition(boolean predicate, Supplier<String> messageSupplier) throws PreconditionViolationException

Assert that the supplied predicate is true.

Parameters
predicate:boolean

the predicate to check

messageSupplier:Supplier<String>

precondition violation message supplier

Annotations
@Contract:false, _ -> fail
Exceptions
PreconditionViolationException:
if the predicate is false
containsNoNullElementsback to summary
public static <T> @Nullable T[] containsNoNullElements(@Nullable T[] array, String message) throws PreconditionViolationException

Assert that the supplied array contains no null elements.

Warning

this method does NOT check if the supplied array is null or empty.

Parameters
array:@Nullable T[]

the array to check

message:String

precondition violation message

Returns:@Nullable T[]

the supplied array as a convenience

Annotations
@Contract:null, _ -> null
Exceptions
PreconditionViolationException:
if the supplied array contains any null elements
See Also
notNull(Object, String)
containsNoNullElementsback to summary
public static <T> @Nullable T[] containsNoNullElements(@Nullable T[] array, Supplier<String> messageSupplier) throws PreconditionViolationException

Assert that the supplied array contains no null elements.

Warning

this method does NOT check if the supplied array is null or empty.

Parameters
array:@Nullable T[]

the array to check

messageSupplier:Supplier<String>

precondition violation message supplier

Returns:@Nullable T[]

the supplied array as a convenience

Annotations
@Contract:null, _ -> null
Exceptions
PreconditionViolationException:
if the supplied array contains any null elements
See Also
notNull(Object, String)
containsNoNullElementsback to summary
public static <T extends Collection<?>> @Nullable T containsNoNullElements(@Nullable T collection, String message) throws PreconditionViolationException

Assert that the supplied collection contains no null elements.

Warning

this method does NOT check if the supplied collection is null or empty.

Parameters
collection:@Nullable T

the collection to check

message:String

precondition violation message

Returns:@Nullable T

the supplied collection as a convenience

Annotations
@Contract:null, _ -> null
Exceptions
PreconditionViolationException:
if the supplied collection contains any null elements
See Also
notNull(Object, String)
containsNoNullElementsback to summary
public static <T extends Collection<?>> @Nullable T containsNoNullElements(@Nullable T collection, Supplier<String> messageSupplier) throws PreconditionViolationException

Assert that the supplied collection contains no null elements.

Warning

this method does NOT check if the supplied collection is null or empty.

Parameters
collection:@Nullable T

the collection to check

messageSupplier:Supplier<String>

precondition violation message supplier

Returns:@Nullable T

the supplied collection as a convenience

Annotations
@Contract:null, _ -> null
Exceptions
PreconditionViolationException:
if the supplied collection contains any null elements
See Also
notNull(Object, String)
notBlankback to summary
public static String notBlank(@Nullable String str, String message) throws PreconditionViolationException

Assert that the supplied String is not blank.

A String is blank if it is null or consists only of whitespace characters.

Parameters
str:@Nullable String

the string to check

message:String

precondition violation message

Returns:String

the supplied string as a convenience

Annotations
@Contract:null, _ -> fail
Exceptions
PreconditionViolationException:
if the supplied string is blank
See Also
notBlank(String, Supplier)
notBlankback to summary
public static String notBlank(@Nullable String str, Supplier<String> messageSupplier) throws PreconditionViolationException

Assert that the supplied String is not blank.

A String is blank if it is null or consists only of whitespace characters.

Parameters
str:@Nullable String

the string to check

messageSupplier:Supplier<String>

precondition violation message supplier

Returns:String

the supplied string as a convenience

Annotations
@Contract:null, _ -> fail
Exceptions
PreconditionViolationException:
if the supplied string is blank
See Also
StringUtils#isNotBlank(String), condition(boolean, Supplier)
notEmptyback to summary
public static int[] notEmpty(@Nullable int[] array, String message) throws PreconditionViolationException

Assert that the supplied array is neither null nor empty.

Parameters
array:@Nullable int[]

the array to check

message:String

precondition violation message

Returns:int[]

the supplied array as a convenience

Annotations
@Contract:null, _ -> fail
@API
status:INTERNAL
since:1.11
Exceptions
PreconditionViolationException:
if the supplied array is null or empty
Since
1.9
See Also
condition(boolean, String)
notEmptyback to summary
public static <T> T[] notEmpty(@Nullable T[] array, String message) throws PreconditionViolationException

Assert that the supplied array is neither null nor empty.

Warning

this method does NOT check if the supplied array contains any null elements.

Parameters
array:@Nullable T[]

the array to check

message:String

precondition violation message

Returns:T[]

the supplied array as a convenience

Annotations
@Contract:null, _ -> fail
Exceptions
PreconditionViolationException:
if the supplied array is null or empty
See Also
containsNoNullElements(Object[], String), condition(boolean, String)
notEmptyback to summary
public static <T> T[] notEmpty(@Nullable T[] array, Supplier<String> messageSupplier) throws PreconditionViolationException

Assert that the supplied array is neither null nor empty.

Warning

this method does NOT check if the supplied array contains any null elements.

Parameters
array:@Nullable T[]

the array to check

messageSupplier:Supplier<String>

precondition violation message supplier

Returns:T[]

the supplied array as a convenience

Annotations
@Contract:null, _ -> fail
Exceptions
PreconditionViolationException:
if the supplied array is null or empty
See Also
containsNoNullElements(Object[], String), condition(boolean, String)
notEmptyback to summary
public static <T extends Collection<?>> T notEmpty(@Nullable T collection, String message) throws PreconditionViolationException

Assert that the supplied Collection is neither null nor empty.

Warning

this method does NOT check if the supplied collection contains any null elements.

Parameters
collection:@Nullable T

the collection to check

message:String

precondition violation message

Returns:T

the supplied collection as a convenience

Annotations
@Contract:null, _ -> fail
Exceptions
PreconditionViolationException:
if the supplied collection is null or empty
See Also
containsNoNullElements(Collection, String), condition(boolean, String)
notEmptyback to summary
public static <T extends Collection<?>> T notEmpty(@Nullable T collection, Supplier<String> messageSupplier) throws PreconditionViolationException

Assert that the supplied Collection is neither null nor empty.

Warning

this method does NOT check if the supplied collection contains any null elements.

Parameters
collection:@Nullable T

the collection to check

messageSupplier:Supplier<String>

precondition violation message supplier

Returns:T

the supplied collection as a convenience

Annotations
@Contract:null, _ -> fail
Exceptions
PreconditionViolationException:
if the supplied collection is null or empty
See Also
containsNoNullElements(Collection, String), condition(boolean, String)
notNullback to summary
public static <T> T notNull(@Nullable T object, String message) throws PreconditionViolationException

Assert that the supplied Object is not null.

Parameters
object:@Nullable T

the object to check

message:String

precondition violation message

Returns:T

the supplied object as a convenience

Annotations
@Contract:null, _ -> fail; !null, _ -> param1
Exceptions
PreconditionViolationException:
if the supplied object is null
See Also
notNull(Object, Supplier)
notNullback to summary
public static <T> T notNull(@Nullable T object, Supplier<String> messageSupplier) throws PreconditionViolationException

Assert that the supplied Object is not null.

Parameters
object:@Nullable T

the object to check

messageSupplier:Supplier<String>

precondition violation message supplier

Returns:T

the supplied object as a convenience

Annotations
@Contract:null, _ -> fail; !null, _ -> param1
Exceptions
PreconditionViolationException:
if the supplied object is null
See Also
condition(boolean, Supplier)