Migrating from FEST Assert 1.4 - alexruiz/fest-assert-2.x GitHub Wiki
FEST-Assert 2.0 is not compatible with FEST-Assert 1.4, most breaking changes are minor ones thus the migration should not be difficult.
Breaking changes
Major changes
- the package change, API is now in
org.fest.assertions.api
- onProperty feature is now extractProperty followed by from
Minor changes
IteratorAssert
has been replaced byIterableAssert
satisfies()
method has been replaced byis()
orhas()
- pick the more readableDelta.delta()
in 1.4 becomesoffset()
in 2.0 (Offset
class)fail()
method has been removed - preferfailBecauseExceptionWasNotThrown(exception class)
.- make your own assertion inheriting from
AbstractAssert
(more details below) description()
in 1.4 becomesdescriptionText()
in 2.0excludes()
in 1.4 becomesdoesNotContain()
in 2.0
Migration detailed steps
- Migrating your own assertions
- failure(String message)
replaced by
fail(String message)` fail()
method removed- Migrating
onProperty
usage toextractProperty
followed by from
Migrating your own assertions
It is simple, instead of inheriting from GenericAssert
you must inherit from AbstractAssert
.
The parameters order in constructor is inverted, for example :
in 1.4 :
public DateTimeAssert(DateTime actual) {
super(DateTimeAssert.class, actual);
}
in 2.0 :
public DateTimeAssert(DateTime actual) {
super(actual, DateTimeAssert.class);
}
failure(String message)
method has been replaced by fail(String message)
:
throw failure(concat("expecting ", objectName, " value not to be null"));
become
Fail.fail(String.format("expecting %s value not to be null", objectName));
description()
in 1.4 becomes descriptionText()
in 2.0
fail()
method removed
We think that when using fail in a test, one should always set a meaningful failure message, thus the removing of no arg fail()
method.
Since fail()
was mainly used when expecting an exception, you can replace it by failBecauseExceptionWasNotThrown(Exception class)
.
Executing :
failBecauseExceptionWasNotThrown(FileNotFoundException.class)
will throw an AssertionError
with message "Expected FileNotFoundException to be thrown"
All fail
methods are available from Assertions
or Fail
class.
onProperty
usage toextractProperty
followed by from(Class)
Migrating In Fest 1.x, you would write :
assertThat(fellowshipOfTheRing).onProperty("name").contains("Gandalf", "Frodo", "Legolas");
In Fest 2.0, it becomes :
// static import of assertThat and extractProperty
import static org.fest.assertions.api.Assertions.*;
assertThat(extractProperty("name").from(fellowshipOfTheRing)).contains("Gandalf", "Frodo", "Legolas");
// more type safe version by specifying the extracted elements type (String)
assertThat(extractProperty("name" , String.class).from(fellowshipOfTheRing)).contains("Gandalf", "Frodo", "Legolas");
We think it was more fluent and easier to read.
Easy replace with regexps
Search with regexp:
assertThat\((.*?)\).onProperty\((.*?)\)
... and replace by :
assertThat(extractProperty(\2).from(\1))
Example :
assertThat(fellowshipOfTheRing).onProperty("name").contains("Gandalf", "Frodo", "Legolas"); // Fest 1.4
... will be changed to :
assertThat(extractProperty("name").from(fellowshipOfTheRing)).contains("Gandalf", "Frodo", "Legolas"); // Fest 2.0