FEST-Assert: Another “assertThat” library for testing
FEST-Assert is another "assertThat" library that aims at making tests more readable. Here is an example:
| Code: |
|
import static org.fest.assertions.Assertions.assertThat; import org.testng.annotations.Test; public class SomeTest { |
We like this approach because:
1. Only one static import is needed (org.fest.assertions.Assertions.assertThat)
2. Allows us to use our IDE’s "auto-complete" feature: we only type "." and we get the possible assertion methods for the value passed to "assertThat"
3. We can chain related assertion methods:
| Code: |
| assertThat(yoda).isInstanceOf(Jedi.class).isEqualTo(foundJedi).isNotEqualTo(possibleSith); |
FEST-Assert can also be extended using Conditions. The following condition verifies that the characters in a String are in uppercase:
| Code: |
|
class UpperCaseCondition extends Condition<String> {
private UpperCaseCondition(String description) { @Override public boolean matches(String value) { public static UpperCaseCondition isUpperCase() { |
This example shows how to use such condition:
| Code: |
| assertThat("hello").as("Greeting").satisfies(isUpperCase()); |
which will fail with the message "[Greeting] expected:Uppercase but was:<’hello’>"
We would appreciate any feedback.






