Top Description Methods
org.assertj.core.api

public Interface Assert<SELF extends Assert<SELF, ACTUAL>, ACTUAL>

extends Descriptable<SELF extends Assert<SELF, ACTUAL>>, ExtensionPoints<SELF extends Assert<SELF, ACTUAL>, ACTUAL>
Known Direct Implementers
org.assertj.core.api.AbstractAssert
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.
Imports
java.util.Comparator, .Date, .List, java.util.function.Consumer, org.assertj.core.presentation.Representation, .StandardRepresentation

Base contract of all assertion objects: the minimum functionality that any assertion object should provide.
Authors
Yvonne Wang, Alex Ruiz, Nicolas François, Mikhail Mazursky

Method Summary

Modifier and TypeMethod and Description
public <
the type of the resulting Assert.
ASSERT extends AbstractAssert<?, ?>
>
ASSERT

Returns:

the narrowed Assert instance.
asInstanceOf
(InstanceOfAssertFactory<?, ASSERT>
the factory which verifies the type and creates the new Assert.
instanceOfAssertFactory
)

Uses an InstanceOfAssertFactory to verify that the actual value is an instance of a given type and to produce a new Assert narrowed to that type.

public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>>

Returns:

a list assertion 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.
public AbstractCharSequenceAssert<?, String>

Returns:

a string assertion object
asString
()

Returns a String assertion for the toString() of the actual value, to allow chaining of String-specific assertions from this call.

public SELF

Returns:

this assertion object.
doesNotHaveSameClassAs
(Object
the object to check type against.
other
)

Verifies that the actual value does not have the same class as the given object.

public SELF

Returns:

this assertion object.
doesNotHaveSameHashCodeAs
(Object
the object to check hashCode against.
other
)

Verifies that the actual object does not have the same hashCode as the given object.

public SELF

Returns:

this assertion object.
doesNotHaveToString
(String
the String to check against.
otherToString
)

Verifies that actual actual.toString() is not equal to the given String.

public SELF

Returns:

this assertion object.
doesNotHaveToString
(String
the format string to use.
expectedStringTemplate
,
Object...
the arguments to interpolate into the format string.
args
)

Verifies that actual actual.toString() is not equal to the given String.

public boolean
equals(Object obj)

Deprecated Throws UnsupportedOperationException if called.
public SELF

Returns:

this assertion object.
hasSameClassAs
(Object
the object to check type against.
other
)

Verifies that the actual value has the same class as the given object.

public SELF

Returns:

this assertion object.
hasSameHashCodeAs
(Object
the object to check hashCode against.
other
)

Verifies that the actual object has the same hashCode as the given object.

public SELF

Returns:

this assertion object.
hasToString
(String
the expected String description of actual.
expectedToString
)

Verifies that actual actual.toString() is equal to the given String.

public SELF

Returns:

this assertion object.
hasToString
(String
the format string to use.
expectedStringTemplate
,
Object...
the arguments to interpolate into the format string.
args
)

Verifies that actual actual.toString() is equal to the given String when formatted with the given arguments.

public SELF

Returns:

this assertion object.
isEqualTo
(Object
the given value to compare the actual value to.
expected
)

Verifies that the actual value is equal to the given one.

public SELF

Returns:

this assertion object.
isExactlyInstanceOf
(Class<?>
the type to check the actual value against.
type
)

Verifies that the actual value is exactly an instance of the given type.

public SELF

Returns:

this assertion object.
isIn
(Object...
the given array to search the actual value in.
values
)

Verifies that the actual value is present in the given array of values.

public SELF

Returns:

this assertion object.
isIn
(Iterable<?>
the given iterable to search the actual value in.
values
)

Verifies that the actual value is present in the given iterable.

public SELF

Returns:

this assertion object.
isInstanceOf
(Class<?>
the type to check the actual value against.
type
)

Verifies that the actual value is an instance of the given type.

public SELF

Returns:

this assertion object.
isInstanceOfAny
(Class<?>...
the types to check the actual value against.
types
)

Verifies that the actual value is an instance of any of the given types.

public <
the generic type to check the actual value against.
T
>
SELF

Returns:

this assertion object.
isInstanceOfSatisfying
(Class<T>
the type to check the actual value against.
type
,
Consumer<T>
the requirements expressed as a Consumer.
requirements
)

Verifies that the actual value is an instance of the given type satisfying the given requirements expressed as a Consumer.

public SELF

Returns:

this assertion object.
isNotEqualTo
(Object
the given value to compare the actual value to.
other
)

Verifies that the actual value is not equal to the given one.

public SELF

Returns:

this assertion object.
isNotExactlyInstanceOf
(Class<?>
the type to check the actual value against.
type
)

Verifies that the actual value is not exactly an instance of given type.

public SELF

Returns:

this assertion object.
isNotIn
(Object...
the given array to search the actual value in.
values
)

Verifies that the actual value is not present in the given array of values.

public SELF

Returns:

this assertion object.
isNotIn
(Iterable<?>
the given iterable to search the actual value in.
values
)

Verifies that the actual value is not present in the given iterable.

public SELF

Returns:

this assertion object.
isNotInstanceOf
(Class<?>
the type to check the actual value against.
type
)

Verifies that the actual value is not an instance of the given type.

public SELF

Returns:

this assertion object.
isNotInstanceOfAny
(Class<?>...
the types to check the actual value against.
types
)

Verifies that the actual value is not an instance of any of the given types.

public SELF

Returns:

this assertion object.
isNotNull
()

Verifies that the actual value is not null.

public SELF

Returns:

this assertion object.
isNotOfAnyClassIn
(Class<?>...
the types to check the actual value against.
types
)

Verifies that the actual value type is not in given types.

public SELF

Returns:

this assertion object.
isNotSameAs
(Object
the given value to compare the actual value to.
other
)

Verifies that the actual value is not the same as the given one, i.e., using == comparison.

public void
isNull()

Verifies that the actual value is null.

public SELF

Returns:

this assertion object.
isOfAnyClassIn
(Class<?>...
the types to check the actual value against.
types
)

Verifies that the actual value type is in given types.

public SELF

Returns:

this assertion object.
isSameAs
(Object
the given value to compare the actual value to.
expected
)

Verifies that the actual value is the same as the given one, i.e., using == comparison.

public SELF

Returns:

this assertion object.
usingComparator
(Comparator<? super ACTUAL>
the comparator to use for the incoming assertion checks.
customComparator
)

Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.

public SELF

Returns:

this assertion object.
usingComparator
(Comparator<? super ACTUAL>
the comparator to use for the incoming assertion checks.
customComparator
,
String
comparator description to be used in assertion error messages
customComparatorDescription
)

Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.

public SELF

Returns:

this assertion object.
usingDefaultComparator
()

Revert to standard comparison for the incoming assertion checks.

public SELF

Returns:

this assertion object.
withRepresentation
(Representation
Describe/represent values in AssertJ error messages.
representation
)

Use the given Representation to describe/represent values in AssertJ error messages.

public SELF

Returns:

this assertion object.
withThreadDumpOnError
()

In case of an assertion error, a thread dump will be printed to System#err.

Inherited from org.assertj.core.api.Descriptable:
asasasdescribedAsdescribedAsdescribedAs
Inherited from org.assertj.core.api.ExtensionPoints:
doesNotHavehasisisNotsatisfies

Method Detail

asInstanceOfback to summary
public <ASSERT extends AbstractAssert<?, ?>> ASSERT asInstanceOf(InstanceOfAssertFactory<?, ASSERT> instanceOfAssertFactory)

Uses an InstanceOfAssertFactory to verify that the actual value is an instance of a given type and to produce a new Assert narrowed to that type.

InstanceOfAssertFactories provides static factories for all the types supported by Assertions#assertThat.

Additional factories can be created with custom InstanceOfAssertFactory instances.

Example:

 // assertions succeeds
Object string = "abc";
assertThat(string).asInstanceOf(InstanceOfAssertFactories.STRING).startsWith("ab");

Object integer = 1;
assertThat(integer).asInstanceOf(InstanceOfAssertFactories.INTEGER).isNotZero();

// assertions fails
assertThat("abc").asInstanceOf(InstanceOfAssertFactories.INTEGER);
Parameters
<ASSERT>
the type of the resulting Assert.
instanceOfAssertFactory:InstanceOfAssertFactory<?, ASSERT>

the factory which verifies the type and creates the new Assert.

Returns:ASSERT

the narrowed Assert instance.

Exceptions
NullPointerException:
if the given factory is null.
Since
3.13.0
See Also
InstanceOfAssertFactory, InstanceOfAssertFactories
asListback to summary
public 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 succeeds
assertThat(sortedListAsObject).asList().isSorted();

Object unsortedListAsObject = Arrays.asList(3, 1, 2);

// assertions fails
assertThat(unsortedListAsObject).asList().isSorted();
Returns:AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>>

a list assertion object

Annotations
@Deprecated
asStringback to summary
public AbstractCharSequenceAssert<?, String> asString()

Returns a String assertion for the toString() of the actual value, to allow chaining of String-specific assertions from this call.

Example :

 Object stringAsObject = "hello world";

// assertion succeeds
assertThat(stringAsObject).asString().contains("hello");

// assertions fails
assertThat(stringAsObject).asString().contains("holla");
Returns:AbstractCharSequenceAssert<?, String>

a string assertion object

doesNotHaveSameClassAsback to summary
public SELF doesNotHaveSameClassAs(Object other)

Verifies that the actual value does not have the same class as the given object.

Example:

 // assertions succeed
assertThat(1).doesNotHaveSameClassAs("abc");
assertThat(new ArrayList<String>()).doesNotHaveSameClassAs(new LinkedList<String>());

// assertions fail
assertThat(1).doesNotHaveSameClassAs(2);
assertThat("abc").doesNotHaveSameClassAs("123");
assertThat(new ArrayList<String>()).doesNotHaveSameClassAs(new ArrayList<Integer>());
Parameters
other:Object

the object to check type against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if actual has the same type as the given object.
NullPointerException:
  • if the actual value is null.
  • if the given object is null.
doesNotHaveSameHashCodeAsback to summary
public SELF doesNotHaveSameHashCodeAs(Object other)

Verifies that the actual object does not have the same hashCode as the given object.

Example:

 // assertions succeed
assertThat(42L).doesNotHaveSameHashCodeAs(2501L);
assertThat("The Force").doesNotHaveSameHashCodeAs("Awakens");

// assertions fail
assertThat(42L).doesNotHaveSameHashCodeAs(42L);
assertThat("The Force").doesNotHaveSameHashCodeAs("The Force");
assertThat(new Jedi("Yoda", "Blue")).doesNotHaveSameHashCodeAs(new Jedi("Yoda", "Blue")); 
Parameters
other:Object

the object to check hashCode against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if the actual object is null.
  • if the actual object has the same hashCode as the given object.
NullPointerException:
if the other object is null.
Since
3.19.0
doesNotHaveToStringback to summary
public SELF doesNotHaveToString(String otherToString)

Verifies that actual actual.toString() is not equal to the given String.

Example :

 CartoonCharacter homer = new CartoonCharacter("Homer");

// Instead of writing ...
assertThat(homer.toString()).isNotEqualTo("Marge");
// ... you can simply write:
assertThat(homer).doesNotHaveToString("Marge");
Parameters
otherToString:String

the String to check against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if actual.toString() result is equal to the given String.
  • if actual is null.
doesNotHaveToStringback to summary
public SELF doesNotHaveToString(String expectedStringTemplate, Object... args)

Verifies that actual actual.toString() is not equal to the given String.

Example:

 Foo foo = new Foo();
Bar bar = new Bar();
FooBarWrapper wrapper = new FooBarWrapper(bar);

assertThat(wrapper).doesNotHaveToString("FooBarWrapper[%s]", foo);
Parameters
expectedStringTemplate:String

the format string to use.

args:Object[]

the arguments to interpolate into the format string.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if actual.toString() result is equal to the given String.
  • if actual is null.
Since
3.25.0
equalsback to summary
public boolean equals(Object obj)

Deprecated

Throws UnsupportedOperationException if called. It is easy to accidentally call equals(Object) instead of isEqualTo(Object).

Annotations
@Override
@Deprecated
Exceptions
UnsupportedOperationException:
if this method is called.
hasSameClassAsback to summary
public SELF hasSameClassAs(Object other)

Verifies that the actual value has the same class as the given object.

Example:

 // assertions succeed
assertThat(1).hasSameClassAs(2);
assertThat("abc").hasSameClassAs("123");
assertThat(new ArrayList<String>()).hasSameClassAs(new ArrayList<Integer>());

// assertions fail
assertThat(1).hasSameClassAs("abc");
assertThat(new ArrayList<String>()).hasSameClassAs(new LinkedList<String>());
Parameters
other:Object

the object to check type against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if actual has not the same type as the given object.
NullPointerException:
  • if the actual value is null.
  • if the given object is null.
hasSameHashCodeAsback to summary
public SELF hasSameHashCodeAs(Object other)

Verifies that the actual object has the same hashCode as the given object.

Example:

 // assertions succeed
assertThat(42L).hasSameHashCodeAs(42L);
assertThat("The Force").hasSameHashCodeAs("The Force");
assertThat(new Jedi("Yoda", "Blue")).hasSameHashCodeAs(new Jedi("Yoda", "Blue"));

// assertions fail
assertThat(42L).hasSameHashCodeAs(2501L);
assertThat(null).hasSameHashCodeAs("The Force");
assertThat("The Force").hasSameHashCodeAs("Awakens");
Parameters
other:Object

the object to check hashCode against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if the actual object is null.
  • if the actual object has not the same hashCode as the given object.
NullPointerException:
if the other object is null.
Since
2.9.0
hasToStringback to summary
public SELF hasToString(String expectedToString)

Verifies that actual actual.toString() is equal to the given String.

Example :

 CartoonCharacter homer = new CartoonCharacter("Homer");

// Instead of writing ...
assertThat(homer.toString()).isEqualTo("Homer");
// ... you can simply write:
assertThat(homer).hasToString("Homer");
Parameters
expectedToString:String

the expected String description of actual.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if actual.toString() result is not equal to the given String.
  • if actual is null.
hasToStringback to summary
public SELF hasToString(String expectedStringTemplate, Object... args)

Verifies that actual actual.toString() is equal to the given String when formatted with the given arguments.

Example:

 Foo foo = new Foo();
FooWrapper wrapper = new FooWrapper(foo);

assertThat(wrapper).hasToString("FooWrapper[foo=%s]", foo); 
Parameters
expectedStringTemplate:String

the format string to use.

args:Object[]

the arguments to interpolate into the format string.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if actual.toString() result is not equal to the given String.
  • if actual is null.
Since
3.25.0
isEqualToback to summary
public SELF isEqualTo(Object expected)

Verifies that the actual value is equal to the given one.

Example:

 // assertions succeed
assertThat("abc").isEqualTo("abc");
assertThat(new HashMap<String, Integer>()).isEqualTo(new HashMap<String, Integer>());

// assertions fail
assertThat("abc").isEqualTo("123");
assertThat(new ArrayList<String>()).isEqualTo(1);
Parameters
expected:Object

the given value to compare the actual value to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual value is not equal to the given one.
isExactlyInstanceOfback to summary
public SELF isExactlyInstanceOf(Class<?> type)

Verifies that the actual value is exactly an instance of the given type.

Example:

 // assertions succeed
assertThat("abc").isExactlyInstanceOf(String.class);
assertThat(new ArrayList<String>()).isExactlyInstanceOf(ArrayList.class);
assertThat(new HashMap<String, Integer>()).isExactlyInstanceOf(HashMap.class);

// assertions fail
assertThat(1).isExactlyInstanceOf(String.class);
assertThat(new ArrayList<String>()).isExactlyInstanceOf(List.class);
assertThat(new HashMap<String, Integer>()).isExactlyInstanceOf(Map.class);
Parameters
type:Class<?>

the type to check the actual value against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual is not exactly an instance of given type.
NullPointerException:
  • if the actual value is null.
  • if the given object is null.
isInback to summary
public SELF isIn(Object... values)

Verifies that the actual value is present in the given array of values.

This assertion always fails if the given array of values is empty.

Example:

 Ring[] elvesRings = new Ring[] { vilya, nenya, narya };

// assertion succeeds
assertThat(nenya).isIn(elvesRings);

// assertions fail
assertThat(oneRing).isIn(elvesRings);
assertThat(oneRing).isIn(new Ring[0]);
Parameters
values:Object[]

the given array to search the actual value in.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given array is null.
AssertionError:
if the actual value is not present in the given array.
isInback to summary
public SELF isIn(Iterable<?> values)

Verifies that the actual value is present in the given iterable.

This assertion always fails if the given iterable is empty.

Example:

 Iterable<Ring> elvesRings = list(vilya, nenya, narya);

// assertion succeeds
assertThat(nenya).isIn(elvesRings);

// assertions fail:
assertThat(oneRing).isIn(elvesRings);
assertThat(oneRing).isIn(emptyList());
Parameters
values:Iterable<?>

the given iterable to search the actual value in.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given collection is null.
AssertionError:
if the actual value is not present in the given iterable.
isInstanceOfback to summary
public SELF isInstanceOf(Class<?> type)

Verifies that the actual value is an instance of the given type.

Example:

 // assertions succeed
assertThat("abc").isInstanceOf(String.class);
assertThat(new HashMap<String, Integer>()).isInstanceOf(HashMap.class);
assertThat(new HashMap<String, Integer>()).isInstanceOf(Map.class);

// assertions fail
assertThat(1).isInstanceOf(String.class);
assertThat(new ArrayList<String>()).isInstanceOf(LinkedList.class);
Parameters
type:Class<?>

the type to check the actual value against.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given type is null.
AssertionError:
  • if the actual value is null.
  • if the actual value is not an instance of the given type.
isInstanceOfAnyback to summary
public SELF isInstanceOfAny(Class<?>... types)

Verifies that the actual value is an instance of any of the given types.

Example:

 // assertions succeed
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 fail
assertThat(1).isInstanceOfAny(Double.class, Float.class);
assertThat(new ArrayList<String>()).isInstanceOfAny(LinkedList.class, Vector.class);
Parameters
types:Class<?>[]

the types to check the actual value against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is not an instance of any of the given types.
NullPointerException:
  • if the given array of types is null.
  • if the given array of types contains nulls.
isInstanceOfSatisfyingback to summary
public <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 a Consumer.

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);
Parameters
<T>
the generic type to check the actual value against.
type:Class<T>

the type to check the actual value against.

requirements:Consumer<T>

the requirements expressed as a Consumer.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
  • if the given type is null.
  • if the given Consumer is null.
AssertionError:
  • if the actual value is null.
  • if the actual value is not an instance of the given type.
isNotEqualToback to summary
public SELF isNotEqualTo(Object other)

Verifies that the actual value is not equal to the given one.

Example:

 // assertions succeed
assertThat("abc").isNotEqualTo("123");
assertThat(new ArrayList<String>()).isNotEqualTo(1);

// assertions fail
assertThat("abc").isNotEqualTo("abc");
assertThat(new HashMap<String, Integer>()).isNotEqualTo(new HashMap<String, Integer>());
Parameters
other:Object

the given value to compare the actual value to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual value is equal to the given one.
isNotExactlyInstanceOfback to summary
public SELF isNotExactlyInstanceOf(Class<?> type)

Verifies that the actual value is not exactly an instance of given type.

Example:

 // assertions succeed
assertThat(1).isNotExactlyInstanceOf(String.class);
assertThat(new ArrayList<String>()).isNotExactlyInstanceOf(List.class);
assertThat(new HashMap<String, Integer>()).isNotExactlyInstanceOf(Map.class);

// assertions fail
assertThat("abc").isNotExactlyInstanceOf(String.class);
assertThat(new ArrayList<String>()).isNotExactlyInstanceOf(ArrayList.class);
assertThat(new HashMap<String, Integer>()).isNotExactlyInstanceOf(HashMap.class);
Parameters
type:Class<?>

the type to check the actual value against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual is exactly an instance of given type.
NullPointerException:
  • if the actual value is null.
  • if the given object is null.
isNotInback to summary
public SELF isNotIn(Object... values)

Verifies that the actual value is not present in the given array of values.

This assertion always succeeds if the given array of values is empty.

Example:

 Ring[] elvesRings = new Ring[] { vilya, nenya, narya };

// assertions succeed
assertThat(oneRing).isNotIn(elvesRings);
assertThat(oneRing).isNotIn(new Ring[0]);

// assertions fails:
assertThat(nenya).isNotIn(elvesRings);
Parameters
values:Object[]

the given array to search the actual value in.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given array is null.
AssertionError:
if the actual value is present in the given array.
isNotInback to summary
public SELF isNotIn(Iterable<?> values)

Verifies that the actual value is not present in the given iterable.

This assertion always succeeds if the given iterable is empty.

Example:

 Iterable<Ring> elvesRings = list(vilya, nenya, narya);

// assertions succeed:
assertThat(oneRing).isNotIn(elvesRings);
assertThat(oneRing).isNotIn(emptyList());

// assertions fails:
assertThat(nenya).isNotIn(elvesRings);
Parameters
values:Iterable<?>

the given iterable to search the actual value in.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given iterable is null.
AssertionError:
if the actual value is present in the given iterable.
isNotInstanceOfback to summary
public SELF isNotInstanceOf(Class<?> type)

Verifies that the actual value is not an instance of the given type.

Example:

 // assertions succeed
assertThat(1).isNotInstanceOf(Double.class);
assertThat(new ArrayList<String>()).isNotInstanceOf(LinkedList.class);

// assertions fail
assertThat("abc").isNotInstanceOf(String.class);
assertThat(new HashMap<String, Integer>()).isNotInstanceOf(HashMap.class);
assertThat(new HashMap<String, Integer>()).isNotInstanceOf(Map.class);
Parameters
type:Class<?>

the type to check the actual value against.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given type is null.
AssertionError:
  • if the actual value is null.
  • if the actual value is an instance of the given type.
isNotInstanceOfAnyback to summary
public SELF isNotInstanceOfAny(Class<?>... types)

Verifies that the actual value is not an instance of any of the given types.

Example:

 // assertions succeed
assertThat(1).isNotInstanceOfAny(Double.class, Float.class);
assertThat(new ArrayList<String>()).isNotInstanceOfAny(LinkedList.class, Vector.class);

// assertions 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:Class<?>[]

the types to check the actual value against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is an instance of any of the given types.
NullPointerException:
  • if the given array of types is null.
  • if the given array of types contains nulls.
isNotNullback to summary
public SELF isNotNull()

Verifies that the actual value is not null.

Example:

 // assertions succeed
assertThat("abc").isNotNull();
assertThat(new HashMap<String, Integer>()).isNotNull();

// assertions fails
String value = null;
assertThat(value).isNotNull();
Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual value is null.
isNotOfAnyClassInback to summary
public SELF isNotOfAnyClassIn(Class<?>... types)

Verifies that the actual value type is not in given types.

Example:

 // assertions succeed
assertThat(new HashMap<String, Integer>()).isNotOfAnyClassIn(Map.class, TreeMap.class);
assertThat(new ArrayList<String>()).isNotOfAnyClassIn(LinkedList.class, List.class);

// assertions fail
assertThat(new HashMap<String, Integer>()).isNotOfAnyClassIn(HashMap.class, TreeMap.class);
assertThat(new ArrayList<String>()).isNotOfAnyClassIn(ArrayList.class, LinkedList.class);
Parameters
types:Class<?>[]

the types to check the actual value against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual value type is in given types.
NullPointerException:
  • if the actual value is null.
  • if the given types is null.
isNotSameAsback to summary
public SELF isNotSameAs(Object other)

Verifies that the actual value is not the same as the given one, i.e., 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:Object

the given value to compare the actual value to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual value is the same as the given one.
isNullback to summary
public void isNull()

Verifies that the actual value is null.

Example:

 String value = null;
// assertion succeeds
assertThat(value).isNull();

// assertions fail
assertThat("abc").isNull();
assertThat(new HashMap<String, Integer>()).isNull();
Exceptions
AssertionError:
if the actual value is not null.
isOfAnyClassInback to summary
public SELF isOfAnyClassIn(Class<?>... types)

Verifies that the actual value type is in given types.

Example:

 // assertions succeed
assertThat(new HashMap<String, Integer>()).isOfAnyClassIn(HashMap.class, TreeMap.class);
assertThat(new ArrayList<String>()).isOfAnyClassIn(ArrayList.class, LinkedList.class);

// assertions fail
assertThat(new HashMap<String, Integer>()).isOfAnyClassIn(TreeMap.class, Map.class);
assertThat(new ArrayList<String>()).isOfAnyClassIn(LinkedList.class, List.class);
Parameters
types:Class<?>[]

the types to check the actual value against.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual value type is not in given type.
NullPointerException:
  • if the actual value is null.
  • if the given types is null.
isSameAsback to summary
public SELF isSameAs(Object expected)

Verifies that the actual value is the same as the given one, i.e., 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:Object

the given value to compare the actual value to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual value is not the same as the given one.
usingComparatorback to summary
public 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:Comparator<? super ACTUAL>

the comparator to use for the incoming assertion checks.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given comparator is null.
usingComparatorback to summary
public 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:Comparator<? super ACTUAL>

the comparator to use for the incoming assertion checks.

customComparatorDescription:String

comparator description to be used in assertion error messages

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given comparator is null.
usingDefaultComparatorback to summary
public 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:SELF

this assertion object.

withRepresentationback to summary
public SELF withRepresentation(Representation representation)

Use the given Representation to describe/represent values in AssertJ error messages.

The usual way to introduce a new Representation is to extend StandardRepresentation and override any existing toStringOf methods that don't suit you. For example you can control Date formatting by overriding StandardRepresentation#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 calling super.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:Representation

Describe/represent values in AssertJ error messages.

Returns:SELF

this assertion object.

withThreadDumpOnErrorback to summary
public SELF withThreadDumpOnError()

In case of an assertion error, a thread dump will be printed to System#err.

Example :

 assertThat("Messi").withThreadDumpOnError().isEqualTo("Ronaldo");
will print a thread dump, something similar to this:
"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:SELF

this assertion object.