Top Description Fields Constructors Methods
org.junit.jupiter.api

public Class AssertionFailureBuilder

extends Object
Class Inheritance
Annotations
@API
status:STABLE
since:5.9
Static Imports
org.apiguardian.api.API.Status.STABLE, org.junit.jupiter.api.AssertionUtils.getCanonicalName

Builder for AssertionFailedErrors.

Using this builder ensures consistency in how failure message are formatted within JUnit Jupiter and for custom user-defined assertions.

Since
5.9
See Also
AssertionFailedError

Field Summary

Modifier and TypeField and Description
private @Nullable Object
private @Nullable Throwable
private @Nullable Object
private boolean
private @Nullable Object
private boolean
private @Nullable String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public AssertionFailureBuilder

Returns:

this builder for method chaining
actual
(@Nullable Object
the actual value; may be null
actual
)

Set the actual value of the assertion.

public static AssertionFailureBuilder
assertionFailure()

Create a new AssertionFailureBuilder.

public AssertionFailedError

Returns:

the built assertion failure
build
()

Build the AssertionFailedError without throwing it.

public void
buildAndThrow()

Build the AssertionFailedError and throw it.

private static String
public AssertionFailureBuilder

Returns:

this builder for method chaining
cause
(@Nullable Throwable
the failure cause; may be null
cause
)

Set the cause of the assertion failure.

public AssertionFailureBuilder

Returns:

this builder for method chaining
expected
(@Nullable Object
the expected value; may be null
expected
)

Set the expected value of the assertion.

private static String
private static String
private static String
public AssertionFailureBuilder

Returns:

this builder for method chaining
includeValuesInMessage
(boolean
whether to include the actual and expected values
includeValuesInMessage
)

Set whether to include the actual and expected values in the generated failure message.

public AssertionFailureBuilder

Returns:

this builder for method chaining
message
(@Nullable Object
the user-defined failure message; may be null
message
)

Set the user-defined message of the assertion.

private static @Nullable String
nullSafeGet(@Nullable Object messageOrSupplier)

public AssertionFailureBuilder

Returns:

this builder for method chaining
reason
(@Nullable String
the failure reason; may be null
reason
)

Set the reason why the assertion failed.

private static String
private static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

actualback to summary
private @Nullable Object actual
causeback to summary
private @Nullable Throwable cause
expectedback to summary
private @Nullable Object expected
includeValuesInMessageback to summary
private boolean includeValuesInMessage
messageback to summary
private @Nullable Object message
mismatchback to summary
private boolean mismatch
reasonback to summary
private @Nullable String reason

Constructor Detail

AssertionFailureBuilderback to summary
private AssertionFailureBuilder()

Method Detail

actualback to summary
public AssertionFailureBuilder actual(@Nullable Object actual)

Set the actual value of the assertion.

Parameters
actual:@Nullable Object

the actual value; may be null

Returns:AssertionFailureBuilder

this builder for method chaining

assertionFailureback to summary
public static AssertionFailureBuilder assertionFailure()

Create a new AssertionFailureBuilder.

buildback to summary
public AssertionFailedError build()

Build the AssertionFailedError without throwing it.

Returns:AssertionFailedError

the built assertion failure

buildAndThrowback to summary
public void buildAndThrow() throws AssertionFailedError

Build the AssertionFailedError and throw it.

Exceptions
AssertionFailedError:
always
buildPrefixback to summary
private static String buildPrefix(@Nullable String message)
causeback to summary
public AssertionFailureBuilder cause(@Nullable Throwable cause)

Set the cause of the assertion failure.

Parameters
cause:@Nullable Throwable

the failure cause; may be null

Returns:AssertionFailureBuilder

this builder for method chaining

expectedback to summary
public AssertionFailureBuilder expected(@Nullable Object expected)

Set the expected value of the assertion.

Parameters
expected:@Nullable Object

the expected value; may be null

Returns:AssertionFailureBuilder

this builder for method chaining

formatClassAndValueback to summary
private static String formatClassAndValue(@Nullable Object value, String valueString)
formatValuesback to summary
private static String formatValues(@Nullable Object expected, @Nullable Object actual)
getClassNameback to summary
private static String getClassName(@Nullable Object obj)
includeValuesInMessageback to summary
public AssertionFailureBuilder includeValuesInMessage(boolean includeValuesInMessage)

Set whether to include the actual and expected values in the generated failure message.

Parameters
includeValuesInMessage:boolean

whether to include the actual and expected values

Returns:AssertionFailureBuilder

this builder for method chaining

messageback to summary
public AssertionFailureBuilder message(@Nullable Object message)

Set the user-defined message of the assertion.

The message may be passed as a Supplier or plain String. If any other type is passed, it is converted to String as per StringUtils#nullSafeToString(Object).

Parameters
message:@Nullable Object

the user-defined failure message; may be null

Returns:AssertionFailureBuilder

this builder for method chaining

nullSafeGetback to summary
private static @Nullable String nullSafeGet(@Nullable Object messageOrSupplier)
reasonback to summary
public AssertionFailureBuilder reason(@Nullable String reason)

Set the reason why the assertion failed.

Parameters
reason:@Nullable String

the failure reason; may be null

Returns:AssertionFailureBuilder

this builder for method chaining

toHashback to summary
private static String toHash(@Nullable Object obj)
toStringback to summary
private static String toString(@Nullable Object obj)