org.fest.assertions
Class Assertions

java.lang.Object
  extended by org.fest.assertions.Assertions

public class Assertions
extends Object

Understands an entry point for assertion methods for different data types. Each method in this class is a static factory for the type-specific assertion objects. The purpose of this class is to make test code more readable.

For example:

 int removed = employees.removeFired();
 assertThat(removed).isZero();

 List<Employee> newEmployees = employees.hired(TODAY);
 assertThat(newEmployees).hasSize(6);
 

Author:
Alex Ruiz, Yvonne Wang, David DIDIER, Ted Young

Constructor Summary
protected Assertions()
          This constructor is protected to make it possible to subclass this class.
 
Method Summary
static BigDecimalAssert assertThat(BigDecimal actual)
          Creates a new instance of BigDecimalAssert.
static BooleanAssert assertThat(boolean actual)
          Creates a new instance of BooleanAssert.
static BooleanAssert assertThat(Boolean actual)
          Creates a new instance of BooleanAssert.
static BooleanArrayAssert assertThat(boolean[] actual)
          Creates a new instance of BooleanArrayAssert.
static ImageAssert assertThat(BufferedImage actual)
          Creates a new instance of ImageAssert.
static ByteAssert assertThat(byte actual)
          Creates a new instance of ByteAssert.
static ByteAssert assertThat(Byte actual)
          Creates a new instance of ByteAssert.
static ByteArrayAssert assertThat(byte[] actual)
          Creates a new instance of ByteArrayAssert.
static CharAssert assertThat(char actual)
          Creates a new instance of CharAssert.
static CharArrayAssert assertThat(char[] actual)
          Creates a new instance of CharArrayAssert.
static CharAssert assertThat(Character actual)
          Creates a new instance of CharAssert.
static CollectionAssert assertThat(Collection<?> actual)
          Creates a new instance of CollectionAssert.
static DoubleAssert assertThat(double actual)
          Creates a new instance of DoubleAssert.
static DoubleAssert assertThat(Double actual)
          Creates a new instance of DoubleAssert.
static DoubleArrayAssert assertThat(double[] actual)
          Creates a new instance of DoubleArrayAssert.
static FileAssert assertThat(File actual)
          Creates a new instance of FileAssert.
static FloatAssert assertThat(float actual)
          Creates a new instance of FloatAssert.
static FloatAssert assertThat(Float actual)
          Creates a new instance of FloatAssert.
static FloatArrayAssert assertThat(float[] actual)
          Creates a new instance of FloatArrayAssert.
static IntAssert assertThat(int actual)
          Creates a new instance of IntAssert.
static IntArrayAssert assertThat(int[] actual)
          Creates a new instance of IntArrayAssert.
static IntAssert assertThat(Integer actual)
          Creates a new instance of IntAssert.
static CollectionAssert assertThat(Iterator<?> actual)
          Creates a new instance of CollectionAssert.
static ListAssert assertThat(List<?> actual)
          Creates a new instance of ListAssert.
static LongAssert assertThat(long actual)
          Creates a new instance of LongAssert.
static LongAssert assertThat(Long actual)
          Creates a new instance of LongAssert.
static LongArrayAssert assertThat(long[] actual)
          Creates a new instance of LongArrayAssert.
static MapAssert assertThat(Map<?,?> actual)
          Creates a new instance of MapAssert.
static ObjectAssert assertThat(Object actual)
          Creates a new instance of ObjectAssert.
static ObjectArrayAssert assertThat(Object[] actual)
          Creates a new instance of ObjectArrayAssert.
static ShortAssert assertThat(short actual)
          Creates a new instance of ShortAssert.
static ShortAssert assertThat(Short actual)
          Creates a new instance of ShortAssert.
static ShortArrayAssert assertThat(short[] actual)
          Creates a new instance of ShortArrayAssert.
static StringAssert assertThat(String actual)
          Creates a new instance of StringAssert.
static
<T extends AssertExtension>
T
assertThat(T assertion)
          Returns the given assertion.
static ThrowableAssert assertThat(Throwable actual)
          Creates a new instance of ThrowableAssert.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Assertions

protected Assertions()
This constructor is protected to make it possible to subclass this class. Since all its methods are static, there is no point on creating a new instance of it.

Method Detail

assertThat

public static BigDecimalAssert assertThat(BigDecimal actual)
Creates a new instance of BigDecimalAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static BooleanAssert assertThat(boolean actual)
Creates a new instance of BooleanAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static BooleanAssert assertThat(Boolean actual)
Creates a new instance of BooleanAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static BooleanArrayAssert assertThat(boolean[] actual)
Creates a new instance of BooleanArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ImageAssert assertThat(BufferedImage actual)
Creates a new instance of ImageAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ByteAssert assertThat(byte actual)
Creates a new instance of ByteAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ByteAssert assertThat(Byte actual)
Creates a new instance of ByteAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ByteArrayAssert assertThat(byte[] actual)
Creates a new instance of ByteArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static CharAssert assertThat(char actual)
Creates a new instance of CharAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static CharAssert assertThat(Character actual)
Creates a new instance of CharAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static CharArrayAssert assertThat(char[] actual)
Creates a new instance of CharArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static CollectionAssert assertThat(Collection<?> actual)
Creates a new instance of CollectionAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ListAssert assertThat(List<?> actual)
Creates a new instance of ListAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.
Since:
1.1

assertThat

public static DoubleAssert assertThat(double actual)
Creates a new instance of DoubleAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static DoubleAssert assertThat(Double actual)
Creates a new instance of DoubleAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static DoubleArrayAssert assertThat(double[] actual)
Creates a new instance of DoubleArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static FileAssert assertThat(File actual)
Creates a new instance of FileAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static FloatAssert assertThat(float actual)
Creates a new instance of FloatAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static FloatAssert assertThat(Float actual)
Creates a new instance of FloatAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static FloatArrayAssert assertThat(float[] actual)
Creates a new instance of FloatArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static IntAssert assertThat(int actual)
Creates a new instance of IntAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static IntAssert assertThat(Integer actual)
Creates a new instance of IntAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static IntArrayAssert assertThat(int[] actual)
Creates a new instance of IntArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static CollectionAssert assertThat(Iterator<?> actual)
Creates a new instance of CollectionAssert.

Parameters:
actual - the value an Iterator that which contents will be added to a new Collection.
Returns:
the created assertion object.

assertThat

public static LongAssert assertThat(long actual)
Creates a new instance of LongAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static LongAssert assertThat(Long actual)
Creates a new instance of LongAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static LongArrayAssert assertThat(long[] actual)
Creates a new instance of LongArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static MapAssert assertThat(Map<?,?> actual)
Creates a new instance of MapAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ObjectAssert assertThat(Object actual)
Creates a new instance of ObjectAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ObjectArrayAssert assertThat(Object[] actual)
Creates a new instance of ObjectArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ShortAssert assertThat(short actual)
Creates a new instance of ShortAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ShortAssert assertThat(Short actual)
Creates a new instance of ShortAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static ShortArrayAssert assertThat(short[] actual)
Creates a new instance of ShortArrayAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static StringAssert assertThat(String actual)
Creates a new instance of StringAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.

assertThat

public static <T extends AssertExtension> T assertThat(T assertion)
Returns the given assertion. This method improves code readability by surrounding the given assertion with "assertThat".

For example, let's assume we have the following custom assertion class:

 public class ServerSocketAssertion implements AssertExtension {
   private final ServerSocket socket;

   public ServerSocketAssertion(ServerSocket socket) {
     this.socket = socket;
   }

   public ServerSocketAssert isConnectedTo(int port) {
     assertThat(socket.isBound()).isTrue();
     assertThat(socket.getLocalPort()).isEqualTo(port);
     assertThat(socket.isClosed()).isFalse();
     return this;
   }
 }
 

We can wrap that assertion with "assertThat" to improve test code readability.

   ServerSocketAssertion socket = new ServerSocketAssertion(server.getSocket());
   assertThat(socket).isConnectedTo(2000);
 

Type Parameters:
T - the generic type of the user-defined assertion.
Parameters:
assertion - the assertion to return.
Returns:
the given assertion.

assertThat

public static ThrowableAssert assertThat(Throwable actual)
Creates a new instance of ThrowableAssert.

Parameters:
actual - the value to be the target of the assertions methods.
Returns:
the created assertion object.


Copyright © 2007-2010 FEST (Fixtures for Easy Software Testing). All Rights Reserved.