Hamcrest is a powerful library for writing custom matchers. Although FEST-Assert and Hamcrest have similar goals, FEST-Assert takes a different approach. Here is an example using Hamcrest (borrowed from Martin Gilday's example posted at the TestNG mailing list):
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; public class SomeTest { @Test public void withHamcrest() { assertTrue(true); int result = 2 + 3; assertThat("calculation", 5, equalTo(result)); assertThat("Hello World", containsString("World")); } }
This is the same example, using FEST's assertions:
import static org.fest.assertions.Assertions.assertThat; import org.testng.annotations.Test; public class SomeTest { @Test public void testApp() { assertThat(true).isTrue(); int result = 2 + 3; assertThat(result).as("calculation").isEqualTo(5); assertThat("Hello World").contains("World"); } }
We like FEST-Assert's approach because:
org.fest.assertions.Assertions.assertThat)
assertThat"
assertThat(yoda).isInstanceOf(Jedi.class) .isEqualTo(foundJedi) .isNotEqualTo(possibleSith);
Which one to use? Hamcrest or FEST-Assert? It is up to you...it depends on the needs of your project and your coding style!