T
- the type of object this condition accepts.public class Condition<T> extends Object implements Descriptable<Condition<T>>
Constructor and Description |
---|
Condition()
Creates a new
. |
Condition(Description description)
Creates a new
. |
Condition(Predicate<T> predicate,
String description,
Object... args)
|
Condition(String description)
Creates a new
. |
Modifier and Type | Method and Description |
---|---|
Condition<T> |
as(Description newDescription)
Sets the description of the assertion that is going to be called after.
|
Condition<T> |
as(String newDescription,
Object... args)
Sets the description of the assertion that is going to be called after.
|
Condition<T> |
describedAs(Description newDescription)
Sets the description of the assertion that is going to be called after.
|
Condition<T> |
describedAs(String newDescription,
Object... args)
Sets the description of the assertion that is going to be called after.
|
Description |
description()
Returns the description of this condition.
|
boolean |
matches(T value)
Verifies that the given value satisfies this condition.
|
String |
toString() |
public Condition()
Condition
. The default description of this condition will the simple name of the
condition's class.public Condition(String description)
Condition
.description
- the description of this condition.NullPointerException
- if the given description is null
.public Condition(Predicate<T> predicate, String description, Object... args)
Condition
with the given Predicate
, the built Condition will be met if
the Predicate is.
You must give a description, it will be used to build a nice error message when the condition fails, you can pass
args to build the description as in String.format(String, Object...)
.
Example:
// build condition with Predicate<String> and set description using String#format pattern.
Condition<String> fairyTale = new Condition<String>(s -> s.startsWith("Once upon a time"), "a %s tale", "fairy");
String littleRedCap = "Once upon a time there was a dear little girl ...";
assertThat(littleRedCap).is(fairyTale);
Error message example:
// unfortunately this assertion fails ... but contact me if you can make it pass :)
assertThat("life").is(fairyTale);
// error message
Expecting:
<"life">
to be <a fairy tale>
predicate
- the Predicate
used to build the condition.description
- the description of this condition.args
- optional parameter if description is a format String.NullPointerException
- if the given Predicate
is null
.NullPointerException
- if the given description is null
.public Condition(Description description)
Condition
.description
- the description of this condition.NullPointerException
- if the given description is null
.public Condition<T> describedAs(String newDescription, Object... args)
You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
Alias for
since "as" is a keyword in Groovy.Descriptable.as(String, Object...)
describedAs
in interface Descriptable<Condition<T>>
newDescription
- the new description to set.args
- optional parameter if description is a format String.this
object.public Condition<T> as(String newDescription, Object... args)
You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
The description follows String.format(String, Object...)
syntax.
Example :
try {
// set a bad age to Mr Frodo which is really 33 years old.
frodo.setAge(50);
// specify a test description (call as() before the assertion !), it supports String format syntax.
assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33);
} catch (AssertionError e) {
assertThat(e).hasMessage("[check Frodo's age] expected:<[33]> but was:<[50]>");
}
as
in interface Descriptable<Condition<T>>
newDescription
- the new description to set.args
- optional parameter if description is a format String.this
object.Descriptable.describedAs(String, Object...)
public Condition<T> describedAs(Description newDescription)
You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
This overloaded version of "describedAs" offers more flexibility than the one taking a String
by allowing
users to pass their own implementation of a description. For example, a description that creates its value lazily,
only when an assertion failure occurs.
describedAs
in interface Descriptable<Condition<T>>
newDescription
- the new description to set.this
object.public Condition<T> as(Description newDescription)
You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
This overloaded version of "describedAs" offers more flexibility than the one taking a String
by allowing
users to pass their own implementation of a description. For example, a description that creates its value lazily,
only when an assertion failure occurs.
as
in interface Descriptable<Condition<T>>
newDescription
- the new description to set.this
object.Descriptable.describedAs(Description)
public Description description()
public boolean matches(T value)
value
- the value to verify.true
if the given value satisfies this condition; false
otherwise.Copyright © 2014–2019 AssertJ. All rights reserved.