Package org.fest.assertions
Class Assertions
java.lang.Object
org.fest.assertions.Assertions
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 Summary
ConstructorsModifierConstructorDescriptionprotected
This constructor is protected to make it possible to subclass this class. -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T> Collection<T>
asCollection
(Iterator<T> iterator) static BooleanAssert
assertThat
(boolean actual) Creates a new instance of
.BooleanAssert
static BooleanArrayAssert
assertThat
(boolean[] actual) Creates a new instance of
.BooleanArrayAssert
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 DoubleAssert
assertThat
(double actual) Creates a new instance of
.DoubleAssert
static DoubleArrayAssert
assertThat
(double[] actual) Creates a new instance of
.DoubleArrayAssert
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 LongAssert
assertThat
(long actual) Creates a new instance of
.LongAssert
static LongArrayAssert
assertThat
(long[] actual) Creates a new instance of
.LongArrayAssert
static ShortAssert
assertThat
(short actual) Creates a new instance of
.ShortAssert
static ShortArrayAssert
assertThat
(short[] actual) Creates a new instance of
.ShortArrayAssert
static ImageAssert
assertThat
(BufferedImage actual) Creates a new instance of
.ImageAssert
static FileAssert
assertThat
(File actual) Creates a new instance of
.FileAssert
static BooleanAssert
assertThat
(Boolean actual) Creates a new instance of
.BooleanAssert
static ByteAssert
assertThat
(Byte actual) Creates a new instance of
.ByteAssert
static CharAssert
assertThat
(Character actual) Creates a new instance of
.CharAssert
static DoubleAssert
assertThat
(Double actual) Creates a new instance of
.DoubleAssert
static FloatAssert
assertThat
(Float actual) Creates a new instance of
.FloatAssert
static IntAssert
assertThat
(Integer actual) Creates a new instance of
.IntAssert
static CollectionAssert
assertThat
(Iterable<?> actual) Creates a new instance of
.CollectionAssert
static LongAssert
assertThat
(Long actual) Creates a new instance of
.LongAssert
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 StringAssert
assertThat
(String actual) Creates a new instance of
.StringAssert
static ThrowableAssert
assertThat
(Throwable actual) Creates a new instance of
.ThrowableAssert
static BigDecimalAssert
assertThat
(BigDecimal actual) Creates a new instance of
.BigDecimalAssert
static CollectionAssert
assertThat
(Collection<?> actual) Creates a new instance of
.CollectionAssert
static CollectionAssert
assertThat
(Iterator<?> actual) Creates a new instance of
.CollectionAssert
static ListAssert
assertThat
(List<?> actual) Creates a new instance of
.ListAssert
static MapAssert
assertThat
(Map<?, ?> actual) Creates a new instance of
.MapAssert
static <T extends AssertExtension>
TassertThat
(T assertion) Returns the given assertion.
-
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
-
assertThat
Creates a new instance of
.BigDecimalAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.BooleanAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.BooleanAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.BooleanArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ImageAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ByteAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ByteAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ByteArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.CharAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.CharAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.CharArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.CollectionAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
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
Creates a new instance of
.DoubleAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.DoubleAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.DoubleArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.FileAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.FloatAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.FloatAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.FloatArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.IntAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.IntAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.IntArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.CollectionAssert
- Parameters:
actual
- anIterable
whose contents will be added to a newCollection
.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.CollectionAssert
- Parameters:
actual
- anIterator
whose contents will be added to a newCollection
.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.LongAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.LongAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.LongArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.MapAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ObjectAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ObjectArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ShortAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ShortAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ShortArrayAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.StringAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-
assertThat
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
Creates a new instance of
.ThrowableAssert
- Parameters:
actual
- the value to be the target of the assertions methods.- Returns:
- the created assertion object.
-