Class AssertionFailedErrorBuilder
- java.lang.Object
-
- com.github.robtimus.junit.support.AssertionFailedErrorBuilder
-
public final class AssertionFailedErrorBuilder extends Object
A builder forAssertionFailedError
s.This class is like an extended version of
AssertionFailureBuilder
. It adds the following additional features:- Providing prefixes for both expected and actual values
- Providing multiple expected and actual values
- Building reasons with value formatting
Any method argument may be
null
unless specified otherwise.- Author:
- Rob Spoor
- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AssertionFailedErrorBuilder.PrefixedValues
An object that can set the possible expected value or actual value of an assertion with a prefix.static class
AssertionFailedErrorBuilder.ReasonBuilder
A builder for the reason why the assertion failed.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AssertionFailedErrorBuilder
actual(Object actual)
Sets the actual value of the assertion.AssertionFailedErrorBuilder
actualValues(Object... actual)
Sets the multiple actual values of the assertion.AssertionFailedErrorBuilder
actualValues(Collection<?> actual)
Sets the multiple actual values of the assertion.static AssertionFailedErrorBuilder
assertionFailedError()
Creates a newAssertionFailedErrorBuilder
.AssertionFailedError
build()
Builds theAssertionFailedError
without throwing it.void
buildAndThrow()
Builds theAssertionFailedError
and throws it.AssertionFailedErrorBuilder
cause(Throwable cause)
Sets the cause of the assertion failure.AssertionFailedErrorBuilder
expected(Object expected)
Sets the expected value of the assertion.AssertionFailedErrorBuilder
expectedMessage(String expected)
Sets a message representing the expected value of the assertion.AssertionFailedErrorBuilder
expectedOneOf(Object... expected)
Sets the possible expected values of the assertion.AssertionFailedErrorBuilder
expectedOneOf(Collection<?> expected)
Sets the possible expected values of the assertion.AssertionFailedErrorBuilder
includeValuesInMessage(boolean includeValuesInMessage)
Sets whether or not to include the actual and expected values in the generated failure message.AssertionFailedErrorBuilder
message(Object message)
Sets the user-defined message of the assertion.AssertionFailedErrorBuilder.PrefixedValues
prefixed(String prefix)
Returns an object that can be used to set expected or actual values with a prefix.AssertionFailedErrorBuilder
reason(String reason)
Sets the reason why the assertion failed.AssertionFailedErrorBuilder.ReasonBuilder
reasonPattern(String pattern)
Returns a builder for the reason why the assertion failed.
-
-
-
Method Detail
-
assertionFailedError
public static AssertionFailedErrorBuilder assertionFailedError()
Creates a newAssertionFailedErrorBuilder
.- Returns:
- The created
AssertionFailedErrorBuilder
.
-
message
public AssertionFailedErrorBuilder message(Object message)
Sets 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 in anull
-safe manner.- Parameters:
message
- The user-defined failure message.- Returns:
- This object.
-
reason
public AssertionFailedErrorBuilder reason(String reason)
Sets the reason why the assertion failed.- Parameters:
reason
- The failure reason.- Returns:
- This object.
-
reasonPattern
public AssertionFailedErrorBuilder.ReasonBuilder reasonPattern(String pattern)
Returns a builder for the reason why the assertion failed.Note that each value added to the reason will be formatted before adding; therefore, each argument should be treated as a string.
- Parameters:
pattern
- The pattern for the reason, with the same syntax asString.format(String, Object...)
.- Returns:
- A builder for the reason why the assertion failed.
- Throws:
NullPointerException
- If the given pattern isnull
.
-
cause
public AssertionFailedErrorBuilder cause(Throwable cause)
Sets the cause of the assertion failure.- Parameters:
cause
- The failure cause.- Returns:
- This object.
-
expected
public AssertionFailedErrorBuilder expected(Object expected)
Sets the expected value of the assertion.- Parameters:
expected
- The expected value.- Returns:
- This object.
-
expectedOneOf
public AssertionFailedErrorBuilder expectedOneOf(Object... expected)
Sets the possible expected values of the assertion. This method will use prefixone of
. To omit the prefix, useprefixed("")
.Note that the
expected value
of the assertion will be a list containing the expected values.- Parameters:
expected
- The expected values.- Returns:
- This object.
-
expectedOneOf
public AssertionFailedErrorBuilder expectedOneOf(Collection<?> expected)
Sets the possible expected values of the assertion. This method will use prefixone of
. To omit the prefix, useprefixed("")
.Note that the
expected value
of the assertion will be a list containing the expected values.- Parameters:
expected
- The expected values.- Returns:
- This object.
- Throws:
NullPointerException
- If the given collection isnull
.
-
expectedMessage
public AssertionFailedErrorBuilder expectedMessage(String expected)
Sets a message representing the expected value of the assertion. This message will be added to the final error message without any value indicators.- Parameters:
expected
- The expected value message.- Returns:
- This object.
-
actual
public AssertionFailedErrorBuilder actual(Object actual)
Sets the actual value of the assertion.- Parameters:
actual
- The actual value.- Returns:
- This object.
-
actualValues
public AssertionFailedErrorBuilder actualValues(Object... actual)
Sets the multiple actual values of the assertion.Note that the
actual value
of the assertion will be a list containing the actual values.- Parameters:
actual
- The actual values.- Returns:
- This object.
-
actualValues
public AssertionFailedErrorBuilder actualValues(Collection<?> actual)
Sets the multiple actual values of the assertion.Note that the
actual value
of the assertion will be a list containing the actual values.- Parameters:
actual
- The actual values.- Returns:
- This object.
- Throws:
NullPointerException
- If the given collection isnull
.
-
prefixed
public AssertionFailedErrorBuilder.PrefixedValues prefixed(String prefix)
Returns an object that can be used to set expected or actual values with a prefix.- Parameters:
prefix
- The prefix to add before the expected or actual value in the generated failure message.- Returns:
- An object that can be used to set expected or actual values with a prefix.
-
includeValuesInMessage
public AssertionFailedErrorBuilder includeValuesInMessage(boolean includeValuesInMessage)
Sets whether or not to include the actual and expected values in the generated failure message.- Parameters:
includeValuesInMessage
-true
to include the actual and expected values, orfalse
to omit them.- Returns:
- This object.
-
buildAndThrow
public void buildAndThrow()
Builds theAssertionFailedError
and throws it.- Throws:
AssertionFailedError
- Always.
-
build
public AssertionFailedError build()
Builds theAssertionFailedError
without throwing it.- Returns:
- The build assertion failure.
-
-