-
Notifications
You must be signed in to change notification settings - Fork 1
Fluent Assertions
Roberto Trunfio edited this page Dec 29, 2016
·
8 revisions
Module it.ozimov:java7-hamcrest-matchers
and it.ozimov:java8-hamcrest-matchers
provide fluent assertions. The class [it.ozimov.cirneco.hamcrest.java7.AssertFluently](http://ozimov.github.io/cirneco/it/ozimov/cirneco/hamcrest/java7/AssertFluently.html)
is essentially
a builder that wraps the common calls to the methods assertThat
and assumeThat
from JUnit.
It is time to provide some examples
import static it.ozimov.cirneco.hamcrest.java7.AssertFluently.*;
import static it.ozimov.cirneco.hamcrest.java7.J7Matchers.not;
import org.junit.Test;
public class UnitTest{
@Test
public void testSomething(){
String actual = "Test"
given(actual)
.because("Actual object "\Test\" must not be equal to \"Something\"")
.assertIsNot("Something");
}
}
The equivalent code using JUnit is:
@Test
public void testSomething(){
String actual = "Test";
assertThat("Actual object "\Test\" must not be equal to \"Something\"")
actual, not("Something"));
}
When using no "actual" object, but just an expression, it can be used the {@linkplain #withReason(String)}, e.g.
@Test
public void testSomething(){
boolean expression = true;
because("Expected the expression to be true")
.assertIs(expression);
}
Another option is to check a boolean
expression directly like follows
@Test
public void testSomething(){
boolean expression = true;
assertIs(expression);
}