Class Assertions

java.lang.Object
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);
 

  • Constructor Details

    • 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 Details

    • asCollection

      private static <T> Collection<T> asCollection(Iterator<T> iterator)
    • 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(Iterable<?> actual)
      Creates a new instance of CollectionAssert.
      Parameters:
      actual - an Iterable whose contents will be added to a new Collection.
      Returns:
      the created assertion object.
    • assertThat

      public static CollectionAssert assertThat(Iterator<?> actual)
      Creates a new instance of CollectionAssert.
      Parameters:
      actual - an Iterator whose 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.