org.assertj.core.api
Class AbstractAssert<S extends AbstractAssert<S,A>,A>

java.lang.Object
  extended by org.assertj.core.api.AbstractAssert<S,A>
Type Parameters:
S - the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.
A - the type of the "actual" value.
All Implemented Interfaces:
Assert<S,A>, Descriptable<S>, ExtensionPoints<S,A>
Direct Known Subclasses:
AbstractBooleanAssert, AbstractCharSequenceAssert, AbstractClassAssert, AbstractComparableAssert, AbstractDateAssert, AbstractEnumerableAssert, AbstractFileAssert, AbstractInputStreamAssert, AbstractIterableAssert, AbstractMapAssert, AbstractObjectArrayAssert, AbstractObjectAssert, AbstractThrowableAssert

public abstract class AbstractAssert<S extends AbstractAssert<S,A>,A>
extends Object
implements Assert<S,A>

Base class for all assertions.

Author:
Alex Ruiz, Joel Costigliola, Mikhail Mazursky, Nicolas François

Field Summary
protected  A actual
           
protected  WritableAssertionInfo info
           
protected  S myself
           
 
Constructor Summary
protected AbstractAssert(A actual, Class<?> selfType)
           
 
Method Summary
 S as(Description description)
          Sets the description of this object.
 S as(String description, Object... args)
          Sets the description of this object supporting String.format(String, Object...) syntax.
 S describedAs(Description description)
          Alias for Descriptable.as(String, Object...) since "as" is a keyword in Groovy.
 S describedAs(String description, Object... args)
          Alias for Descriptable.as(String, Object...) since "as" is a keyword in Groovy.
 String descriptionText()
          The description of this assertion set with describedAs(String, Object...) or describedAs(Description).
 S doesNotHave(Condition<? super A> condition)
          Verifies that the actual value does not satisfy the given condition.
 S doesNotHaveSameClassAs(Object other)
          Verifies that the actual value does not have the same class as the given object.
 boolean equals(Object obj)
          Deprecated. 
protected  void failWithMessage(String errorMessage, Object... arguments)
          Utility method to ease writing custom assertions classes, you can use format specifiers in error message, they will be replaced by the given arguments.
protected  WritableAssertionInfo getWritableAssertionInfo()
          Exposes the WritableAssertionInfo used in the current assertion for better extensibility.
When writing your own assertion class, you can use the returned WritableAssertionInfo to change the error message and still keep the description set by the assertion user.
 S has(Condition<? super A> condition)
          Verifies that the actual value satisfies the given condition.
 int hashCode()
          Always returns 1.
 S hasSameClassAs(Object other)
          Verifies that the actual value has the same class as the given object.
protected  S inBinary()
          Use binary object representation instead of standard representation in error messages.
protected  S inHexadecimal()
          Use hexadecimal object representation instead of standard representation in error messages.
 S is(Condition<? super A> condition)
          Verifies that the actual value satisfies the given condition.
 S isEqualTo(Object expected)
          Verifies that the actual value is equal to the given one.
 S isExactlyInstanceOf(Class<?> type)
          Verifies that the actual value is exactly an instance of the given type.
 S isIn(Iterable<?> values)
          Verifies that the actual value is present in the given values.
 S isIn(Object... values)
          Verifies that the actual value is present in the given array of values.
 S isInstanceOf(Class<?> type)
          Verifies that the actual value is an instance of the given type.
 S isInstanceOfAny(Class<?>... types)
          Verifies that the actual value is an instance of any of the given types.
 S isNot(Condition<? super A> condition)
          Verifies that the actual value does not satisfy the given condition.
 S isNotEqualTo(Object other)
          Verifies that the actual value is not equal to the given one.
 S isNotExactlyInstanceOf(Class<?> type)
          Verifies that the actual value is not exactly an instance of given type.
 S isNotIn(Iterable<?> values)
          Verifies that the actual value is not present in the given values.
 S isNotIn(Object... values)
          Verifies that the actual value is not present in the given array of values.
 S isNotInstanceOf(Class<?> type)
          Verifies that the actual value is not an instance of the given type.
 S isNotInstanceOfAny(Class<?>... types)
          Verifies that the actual value is not an instance of any of the given types.
 S isNotNull()
          Verifies that the actual value is not null.
 S isNotOfAnyClassIn(Class<?>... types)
          Verifies that the actual value type is not in given types.
 S isNotSameAs(Object other)
          Verifies that the actual value is not the same as the given one.
 void isNull()
          Verifies that the actual value is null.
 S isOfAnyClassIn(Class<?>... types)
          Verifies that the actual value type is in given types.
 S isSameAs(Object expected)
          Verifies that the actual value is the same as the given one.
 S overridingErrorMessage(String newErrorMessage, Object... args)
          Overrides AssertJ default error message by the given one.
 S usingComparator(Comparator<? super A> customComparator)
          Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.
 S usingDefaultComparator()
          Revert to standard comparison for incoming assertion checks.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

info

protected final WritableAssertionInfo info

actual

protected final A actual

myself

protected final S extends AbstractAssert<S,A> myself
Constructor Detail

AbstractAssert

protected AbstractAssert(A actual,
                         Class<?> selfType)
Method Detail

getWritableAssertionInfo

protected WritableAssertionInfo getWritableAssertionInfo()
Exposes the WritableAssertionInfo used in the current assertion for better extensibility.
When writing your own assertion class, you can use the returned WritableAssertionInfo to change the error message and still keep the description set by the assertion user.

Returns:
the WritableAssertionInfo used in the current assertion

failWithMessage

protected void failWithMessage(String errorMessage,
                               Object... arguments)
Utility method to ease writing custom assertions classes, you can use format specifiers in error message, they will be replaced by the given arguments.

Moreover, this method honors any description (as(String, Object...) or overridden error message defined by the user ( overridingErrorMessage(String, Object...).

Example :

 public TolkienCharacterAssert hasName(String name) {
   // check that actual TolkienCharacter we want to make assertions on is not null.
   isNotNull();
 
   // check condition
   if (!actual.getName().equals(name)) {
     failWithMessage("Expected character's name to be <%s> but was <%s>", name, actual.getName());
   }
 
   // return the current assertion for method chaining
   return this;
 }
 

Parameters:
errorMessage - the error message to format
arguments - the arguments referenced by the format specifiers in the errorMessage string.

as

public S as(String description,
            Object... args)
Sets the description of this object supporting String.format(String, Object...) syntax.

Example :

 try {
   // set a bad age to Mr Frodo which is really 33 years old.
   frodo.setAge(50);
   // you can specify a test description with as() method or describedAs(), it supports String format args
   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:
as in interface Descriptable<S extends AbstractAssert<S,A>>
Parameters:
description - the new description to set.
args - optional parameter if description is a format String.
Returns:
this object.
See Also:
Descriptable.describedAs(String, Object...)

as

public S as(Description description)
Sets the description of this object. To remove or clear the description, pass a EmptyTextDescription as argument.

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.

Specified by:
as in interface Descriptable<S extends AbstractAssert<S,A>>
Parameters:
description - the new description to set.
Returns:
this object.
See Also:
Descriptable.describedAs(Description)

inHexadecimal

protected S inHexadecimal()
Use hexadecimal object representation instead of standard representation in error messages.

It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned, it is thus impossible to find differences from the standard error message:

With standard message:

 assertThat("µµµ").contains("μμμ");

 java.lang.AssertionError:
 Expecting:
   <"µµµ">
 to contain:
   <"μμμ">
 
With Hexadecimal message:
 assertThat("µµµ").inHexadecimal().contains("μμμ");

 java.lang.AssertionError:
 Expecting:
   <"['00B5', '00B5', '00B5']">
 to contain:
   <"['03BC', '03BC', '03BC']">
 

Returns:
this assertion object.

inBinary

protected S inBinary()
Use binary object representation instead of standard representation in error messages.

Example:

 assertThat(1).inBinary().isEqualTo(2);

 org.junit.ComparisonFailure:
 Expected :0b00000000_00000000_00000000_00000010
 Actual   :0b00000000_00000000_00000000_00000001

Returns:
this assertion object.

describedAs

public S describedAs(String description,
                     Object... args)
Alias for Descriptable.as(String, Object...) since "as" is a keyword in Groovy.

Specified by:
describedAs in interface Descriptable<S extends AbstractAssert<S,A>>
Parameters:
description - the new description to set.
Returns:
this object.

describedAs

public S describedAs(Description description)
Alias for Descriptable.as(String, Object...) since "as" is a keyword in Groovy. To remove or clear the description, pass a EmptyTextDescription as argument.

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.

Specified by:
describedAs in interface Descriptable<S extends AbstractAssert<S,A>>
Parameters:
description - the new description to set.
Returns:
this object.

isEqualTo

public S isEqualTo(Object expected)
Verifies that the actual value is equal to the given one.

Specified by:
isEqualTo in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
expected - the given value to compare the actual value to.
Returns:
this assertion object.

isNotEqualTo

public S isNotEqualTo(Object other)
Verifies that the actual value is not equal to the given one.

Specified by:
isNotEqualTo in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
other - the given value to compare the actual value to.
Returns:
this assertion object.

isNull

public void isNull()
Verifies that the actual value is null.

Specified by:
isNull in interface Assert<S extends AbstractAssert<S,A>,A>

isNotNull

public S isNotNull()
Verifies that the actual value is not null.

Specified by:
isNotNull in interface Assert<S extends AbstractAssert<S,A>,A>
Returns:
this assertion object.

isSameAs

public S isSameAs(Object expected)
Verifies that the actual value is the same as the given one.

Specified by:
isSameAs in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
expected - the given value to compare the actual value to.
Returns:
this assertion object.

isNotSameAs

public S isNotSameAs(Object other)
Verifies that the actual value is not the same as the given one.

Specified by:
isNotSameAs in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
other - the given value to compare the actual value to.
Returns:
this assertion object.

isIn

public S isIn(Object... values)
Verifies that the actual value is present in the given array of values.

Specified by:
isIn in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
values - the given array to search the actual value in.
Returns:
this assertion object.

isNotIn

public S isNotIn(Object... values)
Verifies that the actual value is not present in the given array of values.

Specified by:
isNotIn in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
values - the given array to search the actual value in.
Returns:
this assertion object.

isIn

public S isIn(Iterable<?> values)
Verifies that the actual value is present in the given values.

Specified by:
isIn in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
values - the given iterable to search the actual value in.
Returns:
this assertion object.

isNotIn

public S isNotIn(Iterable<?> values)
Verifies that the actual value is not present in the given values.

Specified by:
isNotIn in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
values - the given iterable to search the actual value in.
Returns:
this assertion object.

is

public S is(Condition<? super A> condition)
Verifies that the actual value satisfies the given condition. This method is an alias for ExtensionPoints.has(Condition).

Specified by:
is in interface ExtensionPoints<S extends AbstractAssert<S,A>,A>
Parameters:
condition - the given condition.
Returns:
this ExtensionPoints object.
See Also:
ExtensionPoints.is(Condition)

isNot

public S isNot(Condition<? super A> condition)
Verifies that the actual value does not satisfy the given condition. This method is an alias for ExtensionPoints.doesNotHave(Condition).

Specified by:
isNot in interface ExtensionPoints<S extends AbstractAssert<S,A>,A>
Parameters:
condition - the given condition.
Returns:
this ExtensionPoints object.
See Also:
ExtensionPoints.isNot(Condition)

has

public S has(Condition<? super A> condition)
Verifies that the actual value satisfies the given condition. This method is an alias for ExtensionPoints.is(Condition) .

Specified by:
has in interface ExtensionPoints<S extends AbstractAssert<S,A>,A>
Parameters:
condition - the given condition.
Returns:
this ExtensionPoints object.
See Also:
ExtensionPoints.is(Condition)

doesNotHave

public S doesNotHave(Condition<? super A> condition)
Verifies that the actual value does not satisfy the given condition. This method is an alias for ExtensionPoints.isNot(Condition).

Specified by:
doesNotHave in interface ExtensionPoints<S extends AbstractAssert<S,A>,A>
Parameters:
condition - the given condition.
Returns:
this ExtensionPoints object.
See Also:
ExtensionPoints.isNot(Condition)

isInstanceOf

public S isInstanceOf(Class<?> type)
Verifies that the actual value is an instance of the given type.

Specified by:
isInstanceOf in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
type - the type to check the actual value against.
Returns:
this assertion object.

isInstanceOfAny

public S isInstanceOfAny(Class<?>... types)
Verifies that the actual value is an instance of any of the given types.

Specified by:
isInstanceOfAny in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
types - the types to check the actual value against.
Returns:
this assertion object.

isNotInstanceOf

public S isNotInstanceOf(Class<?> type)
Verifies that the actual value is not an instance of the given type.

Specified by:
isNotInstanceOf in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
type - the type to check the actual value against.
Returns:
this assertion object.

isNotInstanceOfAny

public S isNotInstanceOfAny(Class<?>... types)
Verifies that the actual value is not an instance of any of the given types.

Specified by:
isNotInstanceOfAny in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
types - the types to check the actual value against.
Returns:
this assertion object.

hasSameClassAs

public S hasSameClassAs(Object other)
Verifies that the actual value has the same class as the given object.

Specified by:
hasSameClassAs in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
other - the object to check type against.
Returns:
this assertion object.

doesNotHaveSameClassAs

public S doesNotHaveSameClassAs(Object other)
Verifies that the actual value does not have the same class as the given object.

Specified by:
doesNotHaveSameClassAs in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
other - the object to check type against.
Returns:
this assertion object.

isExactlyInstanceOf

public S isExactlyInstanceOf(Class<?> type)
Verifies that the actual value is exactly an instance of the given type.

Specified by:
isExactlyInstanceOf in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
type - the type to check the actual value against.
Returns:
this assertion object.

isNotExactlyInstanceOf

public S isNotExactlyInstanceOf(Class<?> type)
Verifies that the actual value is not exactly an instance of given type.

Specified by:
isNotExactlyInstanceOf in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
type - the type to check the actual value against.
Returns:
this assertion object.

isOfAnyClassIn

public S isOfAnyClassIn(Class<?>... types)
Verifies that the actual value type is in given types.

Specified by:
isOfAnyClassIn in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
types - the types to check the actual value against.
Returns:
this assertion object.

isNotOfAnyClassIn

public S isNotOfAnyClassIn(Class<?>... types)
Verifies that the actual value type is not in given types.

Specified by:
isNotOfAnyClassIn in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
types - the types to check the actual value against.
Returns:
this assertion object.

descriptionText

public String descriptionText()
The description of this assertion set with describedAs(String, Object...) or describedAs(Description).

Returns:
the description String representation of this assertion.

overridingErrorMessage

public S overridingErrorMessage(String newErrorMessage,
                                Object... args)
Overrides AssertJ default error message by the given one.

The new error message is built using String.format(String, Object...) if you provide args parameter (if you don't, the error message is taken as it is).

Example :

 assertThat(player.isRookie()).overridingErrorMessage("Expecting Player <%s> to be a rookie but was not.", player)
                              .isTrue();
 

Parameters:
newErrorMessage - the error message that will replace the default one provided by Assertj.
args - the args used to fill error message as in String.format(String, Object...).
Returns:
this assertion object.
Throws:
Exception - see String.format(String, Object...) exception clause.

usingComparator

public S usingComparator(Comparator<? super A> customComparator)
Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.

Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default comparison strategy.

Examples :

 // frodo and sam are instances of Character with Hobbit race (obviously :).
 // raceComparator implements Comparator<Character> 
 assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam); 
 

Specified by:
usingComparator in interface Assert<S extends AbstractAssert<S,A>,A>
Parameters:
customComparator - the comparator to use for incoming assertion checks.
Returns:
this assertion object.

usingDefaultComparator

public S usingDefaultComparator()
Revert to standard comparison for incoming assertion checks.

This method should be used to disable a custom comparison strategy set by calling Assert.usingComparator(Comparator).

Specified by:
usingDefaultComparator in interface Assert<S extends AbstractAssert<S,A>,A>
Returns:
this assertion object.

equals

@Deprecated
public boolean equals(Object obj)
Deprecated. 

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

Specified by:
equals in interface Assert<S extends AbstractAssert<S,A>,A>
Overrides:
equals in class Object
Throws:
UnsupportedOperationException - if this method is called.

hashCode

public int hashCode()
Always returns 1.

Overrides:
hashCode in class Object
Returns:
1.


Copyright © 2013–2015 AssertJ. All rights reserved.