Interface Assert<SELF extends Assert<SELF,ACTUAL>,ACTUAL>
-
- Type Parameters:
SELF
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.ACTUAL
- the type of the "actual" value.
- All Superinterfaces:
Descriptable<SELF>
,ExtensionPoints<SELF,ACTUAL>
- All Known Implementing Classes:
AbstractArrayAssert
,AbstractAssert
,AbstractAtomicFieldUpdaterAssert
,AbstractAtomicReferenceAssert
,AbstractBigDecimalAssert
,AbstractBigIntegerAssert
,AbstractBooleanArrayAssert
,AbstractBooleanAssert
,AbstractByteArrayAssert
,AbstractByteAssert
,AbstractCharacterAssert
,AbstractCharArrayAssert
,AbstractCharSequenceAssert
,AbstractClassAssert
,AbstractComparableAssert
,AbstractCompletableFutureAssert
,AbstractDateAssert
,AbstractDoubleArrayAssert
,AbstractDoubleAssert
,AbstractEnumerableAssert
,AbstractFileAssert
,AbstractFloatArrayAssert
,AbstractFloatAssert
,AbstractFutureAssert
,AbstractInputStreamAssert
,AbstractInstantAssert
,AbstractIntArrayAssert
,AbstractIntegerAssert
,AbstractIterableAssert
,AbstractIterableSizeAssert
,AbstractIteratorAssert
,AbstractListAssert
,AbstractLocalDateAssert
,AbstractLocalDateTimeAssert
,AbstractLocalTimeAssert
,AbstractLongArrayAssert
,AbstractLongAssert
,AbstractMapAssert
,AbstractMapSizeAssert
,AbstractObjectArrayAssert
,AbstractObjectAssert
,AbstractOffsetDateTimeAssert
,AbstractOffsetTimeAssert
,AbstractOptionalAssert
,AbstractOptionalDoubleAssert
,AbstractOptionalIntAssert
,AbstractOptionalLongAssert
,AbstractPathAssert
,AbstractPredicateAssert
,AbstractPredicateLikeAssert
,AbstractShortArrayAssert
,AbstractShortAssert
,AbstractStringAssert
,AbstractTemporalAssert
,AbstractThrowableAssert
,AbstractUriAssert
,AbstractUrlAssert
,AbstractZonedDateTimeAssert
,AtomicBooleanAssert
,AtomicIntegerArrayAssert
,AtomicIntegerAssert
,AtomicIntegerFieldUpdaterAssert
,AtomicLongArrayAssert
,AtomicLongAssert
,AtomicLongFieldUpdaterAssert
,AtomicMarkableReferenceAssert
,AtomicReferenceArrayAssert
,AtomicReferenceAssert
,AtomicReferenceFieldUpdaterAssert
,AtomicStampedReferenceAssert
,BigDecimalAssert
,BigIntegerAssert
,BooleanArrayAssert
,BooleanAssert
,ByteArrayAssert
,ByteAssert
,CharacterAssert
,CharArrayAssert
,CharSequenceAssert
,ClassAssert
,ClassBasedNavigableIterableAssert
,ClassBasedNavigableListAssert
,CompletableFutureAssert
,DateAssert
,DoubleArrayAssert
,DoubleAssert
,DoublePredicateAssert
,FactoryBasedNavigableIterableAssert
,FactoryBasedNavigableListAssert
,FileAssert
,FloatArrayAssert
,FloatAssert
,FutureAssert
,GenericComparableAssert
,InputStreamAssert
,InstantAssert
,IntArrayAssert
,IntegerAssert
,IntPredicateAssert
,IterableAssert
,IterableSizeAssert
,IteratorAssert
,ListAssert
,LocalDateAssert
,LocalDateTimeAssert
,LocalTimeAssert
,LongArrayAssert
,LongAssert
,LongPredicateAssert
,MapAssert
,MapSizeAssert
,ObjectArrayAssert
,ObjectAssert
,OffsetDateTimeAssert
,OffsetTimeAssert
,OptionalAssert
,OptionalDoubleAssert
,OptionalIntAssert
,OptionalLongAssert
,PathAssert
,PredicateAssert
,ProxyableClassAssert
,ProxyableIterableAssert
,ProxyableListAssert
,ProxyableMapAssert
,ProxyableObjectArrayAssert
,ProxyableObjectAssert
,ProxyablePredicateAssert
,RecursiveComparisonAssert
,ShortArrayAssert
,ShortAssert
,StringAssert
,ThrowableAssert
,ThrowableAssertAlternative
,UriAssert
,UrlAssert
,ZonedDateTimeAssert
public interface Assert<SELF extends Assert<SELF,ACTUAL>,ACTUAL> extends Descriptable<SELF>, ExtensionPoints<SELF,ACTUAL>
Base contract of all assertion objects: the minimum functionality that any assertion object should provide.- Author:
- Yvonne Wang, Alex Ruiz, Nicolas François, Mikhail Mazursky
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description <ASSERT extends AbstractAssert<?,?>>
ASSERTasInstanceOf(InstanceOfAssertFactory<?,ASSERT> instanceOfAssertFactory)
Uses anInstanceOfAssertFactory
to verify that the actual value is an instance of a given type and to produce a newAssert
narrowed to that type.AbstractListAssert<?,List<?>,Object,ObjectAssert<Object>>
asList()
Verifies that the actual value is an instance of List, and returns a list assertion, to allow chaining of list-specific assertions from this call.AbstractCharSequenceAssert<?,String>
asString()
Returns a String assertion for thetoString()
of the actual value, to allow chaining of String-specific assertions from this call.SELF
doesNotHaveSameClassAs(Object other)
Verifies that the actual value does not have the same class as the given object.boolean
equals(Object obj)
Deprecated.Throws
if called.UnsupportedOperationException
SELF
hasSameClassAs(Object other)
Verifies that the actual value has the same class as the given object.SELF
hasSameHashCodeAs(Object other)
Verifies that the actual object has the same hashCode as the given object.SELF
hasToString(String expectedToString)
Verifies that actualactual.toString()
is equal to the givenString
.SELF
isEqualTo(Object expected)
Verifies that the actual value is equal to the given one.SELF
isExactlyInstanceOf(Class<?> type)
Verifies that the actual value is exactly an instance of the given type.SELF
isIn(Iterable<?> values)
Verifies that the actual value is present in the given values.SELF
isIn(Object... values)
Verifies that the actual value is present in the given array of values.SELF
isInstanceOf(Class<?> type)
Verifies that the actual value is an instance of the given type.SELF
isInstanceOfAny(Class<?>... types)
Verifies that the actual value is an instance of any of the given types.<T> SELF
isInstanceOfSatisfying(Class<T> type, Consumer<T> requirements)
Verifies that the actual value is an instance of the given type satisfying the given requirements expressed as aConsumer
.SELF
isNotEqualTo(Object other)
Verifies that the actual value is not equal to the given one.SELF
isNotExactlyInstanceOf(Class<?> type)
Verifies that the actual value is not exactly an instance of given type.SELF
isNotIn(Iterable<?> values)
Verifies that the actual value is not present in the given values.SELF
isNotIn(Object... values)
Verifies that the actual value is not present in the given array of values.SELF
isNotInstanceOf(Class<?> type)
Verifies that the actual value is not an instance of the given type.SELF
isNotInstanceOfAny(Class<?>... types)
Verifies that the actual value is not an instance of any of the given types.SELF
isNotNull()
Verifies that the actual value is notnull
.SELF
isNotOfAnyClassIn(Class<?>... types)
Verifies that the actual value type is not in given types.SELF
isNotSameAs(Object other)
Verifies that the actual value is not the same as the given one, ie using == comparison.void
isNull()
Verifies that the actual value isnull
.SELF
isOfAnyClassIn(Class<?>... types)
Verifies that the actual value type is in given types.SELF
isSameAs(Object expected)
Verifies that the actual value is the same as the given one, ie using == comparison.SELF
usingComparator(Comparator<? super ACTUAL> customComparator)
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.SELF
usingComparator(Comparator<? super ACTUAL> customComparator, String customComparatorDescription)
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.SELF
usingDefaultComparator()
Revert to standard comparison for the incoming assertion checks.SELF
withRepresentation(Representation representation)
Use the givenRepresentation
to describe/represent values in AssertJ error messages.SELF
withThreadDumpOnError()
In case of an assertion error, a thread dump will be printed toSystem.err
.-
Methods inherited from interface org.assertj.core.api.Descriptable
as, as, describedAs, describedAs
-
Methods inherited from interface org.assertj.core.api.ExtensionPoints
doesNotHave, has, is, isNot, satisfies
-
-
-
-
Method Detail
-
isEqualTo
SELF isEqualTo(Object expected)
Verifies that the actual value is equal to the given one.Example:
// assertions will pass assertThat("abc").isEqualTo("abc"); assertThat(new HashMap<String, Integer>()).isEqualTo(new HashMap<String, Integer>()); // assertions will fail assertThat("abc").isEqualTo("123"); assertThat(new ArrayList<String>()).isEqualTo(1);
- Parameters:
expected
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual value is not equal to the given one.
-
isNotEqualTo
SELF isNotEqualTo(Object other)
Verifies that the actual value is not equal to the given one.Example:
// assertions will pass assertThat("abc").isNotEqualTo("123"); assertThat(new ArrayList<String>()).isNotEqualTo(1); // assertions will fail assertThat("abc").isNotEqualTo("abc"); assertThat(new HashMap<String, Integer>()).isNotEqualTo(new HashMap<String, Integer>());
- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual value is equal to the given one.
-
isNull
void isNull()
Verifies that the actual value isnull
.Example:
String value = null; // assertion will pass assertThat(value).isNull(); // assertions will fail assertThat("abc").isNull(); assertThat(new HashMap<String, Integer>()).isNull();
- Throws:
AssertionError
- if the actual value is notnull
.
-
isNotNull
SELF isNotNull()
Verifies that the actual value is notnull
.Example:
// assertion will pass assertThat("abc").isNotNull(); assertThat(new HashMap<String, Integer>()).isNotNull(); // assertion will fail String value = null; assertThat(value).isNotNull();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual value isnull
.
-
isSameAs
SELF isSameAs(Object expected)
Verifies that the actual value is the same as the given one, ie using == comparison.Example:
// Name is a class with first and last fields, two Names are equals if both first and last are equals. Name tyrion = new Name("Tyrion", "Lannister"); Name alias = tyrion; Name clone = new Name("Tyrion", "Lannister"); // assertions succeed: assertThat(tyrion).isSameAs(alias) .isEqualTo(clone); // assertion fails: assertThat(tyrion).isSameAs(clone);
- Parameters:
expected
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual value is not the same as the given one.
-
isNotSameAs
SELF isNotSameAs(Object other)
Verifies that the actual value is not the same as the given one, ie using == comparison.Example:
// Name is a class with first and last fields, two Names are equals if both first and last are equals. Name tyrion = new Name("Tyrion", "Lannister"); Name alias = tyrion; Name clone = new Name("Tyrion", "Lannister"); // assertions succeed: assertThat(clone).isNotSameAs(tyrion) .isEqualTo(tyrion); // assertion fails: assertThat(alias).isNotSameAs(tyrion);
- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual value is the same as the given one.
-
isIn
SELF isIn(Object... values)
Verifies that the actual value is present in the given array of values.Example:
Ring[] elvesRings = new Ring[] { vilya, nenya, narya }; // assertion will pass: assertThat(nenya).isIn(elvesRings); // assertion will fail: assertThat(oneRing).isIn(elvesRings);
- Parameters:
values
- the given array to search the actual value in.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given array isnull
.IllegalArgumentException
- if the given array is empty.AssertionError
- if the actual value is not present in the given array.
-
isNotIn
SELF isNotIn(Object... values)
Verifies that the actual value is not present in the given array of values.Example:
Ring[] elvesRings = new Ring[] { vilya, nenya, narya }; // assertion will pass: assertThat(oneRing).isNotIn(elvesRings); // assertion will fail: assertThat(nenya).isNotIn(elvesRings);
- Parameters:
values
- the given array to search the actual value in.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given array isnull
.IllegalArgumentException
- if the given array is empty.AssertionError
- if the actual value is present in the given array.
-
isIn
SELF isIn(Iterable<?> values)
Verifies that the actual value is present in the given values.Example:
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertion will pass: assertThat(nenya).isIn(elvesRings); // assertion will fail: assertThat(oneRing).isIn(elvesRings);
- Parameters:
values
- the given iterable to search the actual value in.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given collection isnull
.IllegalArgumentException
- if the given collection is empty.AssertionError
- if the actual value is not present in the given collection.
-
isNotIn
SELF isNotIn(Iterable<?> values)
Verifies that the actual value is not present in the given values.Example:
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertion will pass: assertThat(oneRing).isNotIn(elvesRings); // assertion will fail: assertThat(nenya).isNotIn(elvesRings);
- Parameters:
values
- the given iterable to search the actual value in.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given collection isnull
.IllegalArgumentException
- if the given collection is empty.AssertionError
- if the actual value is present in the given collection.
-
usingComparator
SELF usingComparator(Comparator<? super ACTUAL> customComparator)
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.The custom comparator is bound to assertion instance, meaning that if a new assertion instance is created, the default comparison strategy will be used.
Examples :
// frodo and sam are instances of Character with Hobbit race (obviously :). // raceComparator implements Comparator<Character> assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam);
- Parameters:
customComparator
- the comparator to use for the incoming assertion checks.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given comparator isnull
.
-
usingComparator
SELF usingComparator(Comparator<? super ACTUAL> customComparator, String customComparatorDescription)
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.The custom comparator is bound to assertion instance, meaning that if a new assertion instance is created, the default comparison strategy will be used.
Examples :
// frodo and sam are instances of Character with Hobbit race (obviously :). // raceComparator implements Comparator<Character> assertThat(frodo).usingComparator(raceComparator, "Hobbit Race Comparator").isEqualTo(sam);
- Parameters:
customComparator
- the comparator to use for the incoming assertion checks.customComparatorDescription
- comparator description to be used in assertion error messages- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given comparator isnull
.
-
usingDefaultComparator
SELF usingDefaultComparator()
Revert to standard comparison for the incoming assertion checks.This method should be used to disable a custom comparison strategy set by calling
usingComparator
.- Returns:
this
assertion object.
-
asInstanceOf
<ASSERT extends AbstractAssert<?,?>> ASSERT asInstanceOf(InstanceOfAssertFactory<?,ASSERT> instanceOfAssertFactory)
Uses anInstanceOfAssertFactory
to verify that the actual value is an instance of a given type and to produce a newAssert
narrowed to that type.InstanceOfAssertFactories
provides static factories for all the types supported byAssertions#assertThat
.Additional factories can be created with custom
InstanceOfAssertFactory
instances.Example:
// assertions will pass Object string = "abc"; assertThat(string).asInstanceOf(InstanceOfAssertFactories.STRING).startsWith("ab"); Object integer = 1; assertThat(integer).asInstanceOf(InstanceOfAssertFactories.INTEGER).isNotZero(); // assertion will fail assertThat("abc").asInstanceOf(InstanceOfAssertFactories.INTEGER);
- Type Parameters:
ASSERT
- the type of the resultingAssert
.- Parameters:
instanceOfAssertFactory
- the factory which verifies the type and creates the newAssert
.- Returns:
- the narrowed
Assert
instance. - Since:
- 3.13.0
- See Also:
InstanceOfAssertFactory
,InstanceOfAssertFactories
-
isInstanceOf
SELF isInstanceOf(Class<?> type)
Verifies that the actual value is an instance of the given type.Example:
// assertions will pass assertThat("abc").isInstanceOf(String.class); assertThat(new HashMap<String, Integer>()).isInstanceOf(HashMap.class); assertThat(new HashMap<String, Integer>()).isInstanceOf(Map.class); // assertions will fail assertThat(1).isInstanceOf(String.class); assertThat(new ArrayList<String>()).isInstanceOf(LinkedList.class);
- Parameters:
type
- the type to check the actual value against.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given type isnull
.AssertionError
- if the actual value isnull
.AssertionError
- if the actual value is not an instance of the given type.
-
isInstanceOfSatisfying
<T> SELF isInstanceOfSatisfying(Class<T> type, Consumer<T> requirements)
Verifies that the actual value is an instance of the given type satisfying the given requirements expressed as aConsumer
.This is useful to perform a group of assertions on a single object after checking its runtime type.
Example:
// second constructor parameter is the light saber color Object yoda = new Jedi("Yoda", "Green"); Object luke = new Jedi("Luke Skywalker", "Green"); Consumer<Jedi> jediRequirements = jedi -> { assertThat(jedi.getLightSaberColor()).isEqualTo("Green"); assertThat(jedi.getName()).doesNotContain("Dark"); }; // assertions succeed: assertThat(yoda).isInstanceOfSatisfying(Jedi.class, jediRequirements); assertThat(luke).isInstanceOfSatisfying(Jedi.class, jediRequirements); // assertions fail: Jedi vader = new Jedi("Vader", "Red"); assertThat(vader).isInstanceOfSatisfying(Jedi.class, jediRequirements); // not a Jedi ! assertThat("foo").isInstanceOfSatisfying(Jedi.class, jediRequirements);
- Type Parameters:
T
- the generic type to check the actual value against.- Parameters:
type
- the type to check the actual value against.requirements
- the requirements expressed as aConsumer
.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given type isnull
.NullPointerException
- if the given Consumer isnull
.AssertionError
- if the actual value isnull
.AssertionError
- if the actual value is not an instance of the given type.
-
isInstanceOfAny
SELF isInstanceOfAny(Class<?>... types)
Verifies that the actual value is an instance of any of the given types.Example:
// assertions will pass assertThat("abc").isInstanceOfAny(String.class, Integer.class); assertThat(new ArrayList<String>()).isInstanceOfAny(LinkedList.class, ArrayList.class); assertThat(new HashMap<String, Integer>()).isInstanceOfAny(TreeMap.class, Map.class); // assertions will fail assertThat(1).isInstanceOfAny(Double.class, Float.class); assertThat(new ArrayList<String>()).isInstanceOfAny(LinkedList.class, Vector.class);
- Parameters:
types
- the types to check the actual value against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual value isnull
.AssertionError
- if the actual value is not an instance of any of the given types.NullPointerException
- if the given array of types isnull
.NullPointerException
- if the given array of types containsnull
s.
-
isNotInstanceOf
SELF isNotInstanceOf(Class<?> type)
Verifies that the actual value is not an instance of the given type.Example:
// assertions will pass assertThat(1).isNotInstanceOf(Double.class); assertThat(new ArrayList<String>()).isNotInstanceOf(LinkedList.class); // assertions will fail assertThat("abc").isNotInstanceOf(String.class); assertThat(new HashMap<String, Integer>()).isNotInstanceOf(HashMap.class); assertThat(new HashMap<String, Integer>()).isNotInstanceOf(Map.class);
- Parameters:
type
- the type to check the actual value against.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given type isnull
.AssertionError
- if the actual value isnull
.AssertionError
- if the actual value is an instance of the given type.
-
isNotInstanceOfAny
SELF isNotInstanceOfAny(Class<?>... types)
Verifies that the actual value is not an instance of any of the given types.Example:
// assertions will pass assertThat(1).isNotInstanceOfAny(Double.class, Float.class); assertThat(new ArrayList<String>()).isNotInstanceOfAny(LinkedList.class, Vector.class); // assertions will fail assertThat(1).isNotInstanceOfAny(Double.class, Integer.class); assertThat(new ArrayList<String>()).isNotInstanceOfAny(LinkedList.class, ArrayList.class); assertThat(new HashMap<String, Integer>()).isNotInstanceOfAny(TreeMap.class, Map.class);
- Parameters:
types
- the types to check the actual value against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual value isnull
.AssertionError
- if the actual value is an instance of any of the given types.NullPointerException
- if the given array of types isnull
.NullPointerException
- if the given array of types containsnull
s.
-
hasSameClassAs
SELF hasSameClassAs(Object other)
Verifies that the actual value has the same class as the given object.Example:
// assertions will pass assertThat(1).hasSameClassAs(2); assertThat("abc").hasSameClassAs("123"); assertThat(new ArrayList<String>()).hasSameClassAs(new ArrayList<Integer>()); // assertions will fail assertThat(1).hasSameClassAs("abc"); assertThat(new ArrayList<String>()).hasSameClassAs(new LinkedList<String>());
- Parameters:
other
- the object to check type against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual has not the same type has the given object.NullPointerException
- if the actual value is null.NullPointerException
- if the given object is null.
-
hasToString
SELF hasToString(String expectedToString)
Verifies that actualactual.toString()
is equal to the givenString
.Example :
CartoonCharacter homer = new CartoonCharacter("Homer"); // Instead of writing ... assertThat(homer.toString()).isEqualTo("Homer"); // ... you can simply write: assertThat(homer).hasToString("Homer");
- Parameters:
expectedToString
- the expected String description of actual.- Returns:
- this assertion object.
- Throws:
AssertionError
- ifactual.toString()
result is not to the givenString
.AssertionError
- if actual isnull
.
-
doesNotHaveSameClassAs
SELF doesNotHaveSameClassAs(Object other)
Verifies that the actual value does not have the same class as the given object.Example:
// assertions will pass assertThat(1).doesNotHaveSameClassAs("abc"); assertThat(new ArrayList<String>()).doesNotHaveSameClassAs(new LinkedList<String>()); // assertions will fail assertThat(1).doesNotHaveSameClassAs(2); assertThat("abc").doesNotHaveSameClassAs("123"); assertThat(new ArrayList<String>()).doesNotHaveSameClassAs(new ArrayList<Integer>());
- Parameters:
other
- the object to check type against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual has the same type has the given object.NullPointerException
- if the actual value is null.NullPointerException
- if the given object is null.
-
isExactlyInstanceOf
SELF isExactlyInstanceOf(Class<?> type)
Verifies that the actual value is exactly an instance of the given type.Example:
// assertions will pass assertThat("abc").isExactlyInstanceOf(String.class); assertThat(new ArrayList<String>()).isExactlyInstanceOf(ArrayList.class); assertThat(new HashMap<String, Integer>()).isExactlyInstanceOf(HashMap.class); // assertions will fail assertThat(1).isExactlyInstanceOf(String.class); assertThat(new ArrayList<String>()).isExactlyInstanceOf(List.class); assertThat(new HashMap<String, Integer>()).isExactlyInstanceOf(Map.class);
- Parameters:
type
- the type to check the actual value against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual is not exactly an instance of given type.NullPointerException
- if the actual value is null.NullPointerException
- if the given object is null.
-
isNotExactlyInstanceOf
SELF isNotExactlyInstanceOf(Class<?> type)
Verifies that the actual value is not exactly an instance of given type.Example:
// assertions will pass assertThat(1).isNotExactlyInstanceOf(String.class); assertThat(new ArrayList<String>()).isNotExactlyInstanceOf(List.class); assertThat(new HashMap<String, Integer>()).isNotExactlyInstanceOf(Map.class); // assertions will fail assertThat("abc").isNotExactlyInstanceOf(String.class); assertThat(new ArrayList<String>()).isNotExactlyInstanceOf(ArrayList.class); assertThat(new HashMap<String, Integer>()).isNotExactlyInstanceOf(HashMap.class);
- Parameters:
type
- the type to check the actual value against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual is exactly an instance of given type.NullPointerException
- if the actual value is null.NullPointerException
- if the given object is null.
-
isOfAnyClassIn
SELF isOfAnyClassIn(Class<?>... types)
Verifies that the actual value type is in given types.Example:
// assertions will pass assertThat(new HashMap<String, Integer>()).isOfAnyClassIn(HashMap.class, TreeMap.class); assertThat(new ArrayList<String>()).isOfAnyClassIn(ArrayList.class, LinkedList.class); // assertions will fail assertThat(new HashMap<String, Integer>()).isOfAnyClassIn(TreeMap.class, Map.class); assertThat(new ArrayList<String>()).isOfAnyClassIn(LinkedList.class, List.class);
- Parameters:
types
- the types to check the actual value against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual value type is not in given type.NullPointerException
- if the actual value is null.NullPointerException
- if the given types is null.
-
isNotOfAnyClassIn
SELF isNotOfAnyClassIn(Class<?>... types)
Verifies that the actual value type is not in given types.Example:
// assertions will pass assertThat(new HashMap<String, Integer>()).isNotOfAnyClassIn(Map.class, TreeMap.class); assertThat(new ArrayList<String>()).isNotOfAnyClassIn(LinkedList.class, List.class); // assertions will fail assertThat(new HashMap<String, Integer>()).isNotOfAnyClassIn(HashMap.class, TreeMap.class); assertThat(new ArrayList<String>()).isNotOfAnyClassIn(ArrayList.class, LinkedList.class);
- Parameters:
types
- the types to check the actual value against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual value type is in given types.NullPointerException
- if the actual value is null.NullPointerException
- if the given types is null.
-
asList
AbstractListAssert<?,List<?>,Object,ObjectAssert<Object>> asList()
Verifies that the actual value is an instance of List, and returns a list assertion, to allow chaining of list-specific assertions from this call.Example :
Object sortedListAsObject = Arrays.asList(1, 2, 3); // assertion will pass assertThat(sortedListAsObject).asList().isSorted(); Object unsortedListAsObject = Arrays.asList(3, 1, 2); // assertion will fail assertThat(unsortedListAsObject).asList().isSorted();
- Returns:
- a list assertion object
-
asString
AbstractCharSequenceAssert<?,String> asString()
Returns a String assertion for thetoString()
of the actual value, to allow chaining of String-specific assertions from this call.Example :
Object stringAsObject = "hello world"; // assertion will pass assertThat(stringAsObject).asString().contains("hello"); // assertion will fail assertThat(stringAsObject).asString().contains("holla");
- Returns:
- a string assertion object
-
equals
@Deprecated boolean equals(Object obj)
Deprecated.Throws
if called. It is easy to accidentally callUnsupportedOperationException
equals(Object)
instead of
.isEqualTo(Object)
- Overrides:
equals
in classObject
- Throws:
UnsupportedOperationException
- if this method is called.
-
withThreadDumpOnError
SELF withThreadDumpOnError()
In case of an assertion error, a thread dump will be printed toSystem.err
.Example :
will print a thread dump, something similar to this:assertThat("Messi").withThreadDumpOnError().isEqualTo("Ronaldo");
"JDWP Command Reader" java.lang.Thread.State: RUNNABLE "JDWP Event Helper Thread" java.lang.Thread.State: RUNNABLE "JDWP Transport Listener: dt_socket" java.lang.Thread.State: RUNNABLE "Signal Dispatcher" java.lang.Thread.State: RUNNABLE "Finalizer" java.lang.Thread.State: WAITING at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189) "Reference Handler" java.lang.Thread.State: WAITING at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:503) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133) "main" java.lang.Thread.State: RUNNABLE at sun.management.ThreadImpl.dumpThreads0(Native Method) at sun.management.ThreadImpl.dumpAllThreads(ThreadImpl.java:446) at org.assertj.core.internal.Failures.threadDumpDescription(Failures.java:193) at org.assertj.core.internal.Failures.printThreadDumpIfNeeded(Failures.java:141) at org.assertj.core.internal.Failures.failure(Failures.java:91) at org.assertj.core.internal.Objects.assertEqual(Objects.java:314) at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:198) at org.assertj.examples.ThreadDumpOnErrorExample.main(ThreadDumpOnErrorExample.java:28)
- Returns:
- this assertion object.
-
withRepresentation
SELF withRepresentation(Representation representation)
Use the givenRepresentation
to describe/represent values in AssertJ error messages.The usual way to introduce a new
Representation
is to extendStandardRepresentation
and override any existingtoStringOf
methods that don't suit you. For example you can controlDate
formatting by overridingStandardRepresentation.toStringOf(Date)
).You can also control other types format by overriding
StandardRepresentation.toStringOf(Object)
) calling your formatting method first and then fall back to the default representation by callingsuper.toStringOf(Object)
.Example :
private class Example {} private class CustomRepresentation extends StandardRepresentation { // override needed to hook specific formatting @Override public String toStringOf(Object o) { if (o instanceof Example) return "Example"; // fall back to default formatting return super.toStringOf(o); } // change String representation @Override protected String toStringOf(String s) { return "$" + s + "$"; } } // next assertion fails with error : "expected:<[null]> but was:<[Example]>" Example example = new Example(); assertThat(example).withRepresentation(new CustomRepresentation()) .isNull(); // example is not null ! // next assertion fails ... assertThat("foo").withRepresentation(new CustomRepresentation()) .startsWith("bar"); // ... with error : Expecting: <$foo$> to start with: <$bar$>
- Parameters:
representation
- Describe/represent values in AssertJ error messages.- Returns:
- this assertion object.
-
hasSameHashCodeAs
SELF hasSameHashCodeAs(Object other)
Verifies that the actual object has the same hashCode as the given object.Example:
// assertions will pass assertThat(42L).hasSameHashCodeAs(42L); assertThat("The Force").hasSameHashCodeAs("The Force"); assertThat(new Jedi("Yoda", "Blue")).hasSameHashCodeAs(new Jedi("Yoda", "Blue")); // assertions will fail assertThat(42L).hasSameHashCodeAs(2501L); assertThat(null).hasSameHashCodeAs("The Force"); assertThat("The Force").hasSameHashCodeAs(null);
- Parameters:
other
- the object to check hashCode against.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual object is null.NullPointerException
- if the other object is null.AssertionError
- if the actual object has not the same hashCode as the given object.- Since:
- 2.9.0
-
-