Class Condition<T>
- java.lang.Object
-
- org.assertj.core.api.Condition<T>
-
- Type Parameters:
T- the type of object this condition accepts.
- All Implemented Interfaces:
Descriptable<Condition<T>>
- Direct Known Subclasses:
HamcrestCondition,Join,Negative
public class Condition<T> extends Object implements Descriptable<Condition<T>>
A condition to be met by an object.- Author:
- Yvonne Wang, Alex Ruiz
-
-
Field Summary
Fields Modifier and Type Field Description (package private) Descriptiondescriptionprivate Predicate<T>predicate
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Condition<T>as(String newDescription, Object... args)Sets the description of the assertion that is going to be called after.Condition<T>as(Description newDescription)Sets the description of the assertion that is going to be called after.private voidcheckPredicate(Predicate<T> predicate)Condition<T>describedAs(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.Descriptiondescription()Returns the description of this condition.booleanmatches(T value)Verifies that the given value satisfies this condition.StringtoString()
-
-
-
Field Detail
-
description
Description description
-
-
Constructor Detail
-
Condition
public Condition()
Creates a new. The default description of this condition will the simple name of the condition's class.Condition
-
Condition
public Condition(String description)
Creates a new.Condition- Parameters:
description- the description of this condition.- Throws:
NullPointerException- if the given description isnull.
-
Condition
public Condition(Predicate<T> predicate, String description, Object... args)
Creates a newwith the givenConditionPredicate, 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:
Error message 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);// 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>- Parameters:
predicate- thePredicateused to build the condition.description- the description of this condition.args- optional parameter if description is a format String.- Throws:
NullPointerException- if the givenPredicateisnull.NullPointerException- if the given description isnull.
-
Condition
public Condition(Description description)
Creates a new.Condition- Parameters:
description- the description of this condition.- Throws:
NullPointerException- if the given description isnull.
-
-
Method Detail
-
describedAs
public Condition<T> describedAs(String newDescription, Object... args)
Sets the description of the assertion that is going to be called after.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...)- Specified by:
describedAsin interfaceDescriptable<T>- Parameters:
newDescription- the new description to set.args- optional parameter if description is a format String.- Returns:
thisobject.
-
as
public Condition<T> as(String newDescription, Object... args)
Sets the description of the assertion that is going to be called after.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]>"); }- Specified by:
asin interfaceDescriptable<T>- Parameters:
newDescription- the new description to set.args- optional parameter if description is a format String.- Returns:
thisobject.- See Also:
Descriptable.describedAs(String, Object...)
-
describedAs
public Condition<T> describedAs(Description newDescription)
Sets the description of the assertion that is going to be called after.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
Stringby 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.- Specified by:
describedAsin interfaceDescriptable<T>- Parameters:
newDescription- the new description to set.- Returns:
thisobject.
-
as
public Condition<T> as(Description newDescription)
Sets the description of the assertion that is going to be called after.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
Stringby 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.- Specified by:
asin interfaceDescriptable<T>- Parameters:
newDescription- the new description to set.- Returns:
thisobject.- See Also:
Descriptable.describedAs(Description)
-
description
public Description description()
Returns the description of this condition.- Returns:
- the description of this condition.
-
matches
public boolean matches(T value)
Verifies that the given value satisfies this condition.- Parameters:
value- the value to verify.- Returns:
trueif the given value satisfies this condition;falseotherwise.
-
-