Assert numbers

equalTo

Given the following example:

<code lang="java">
  public void testAssert() {
    int n = 4;
    assertThat("Assert that n is equal to 5", n, Matchers
        .equalTo(5));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert that n is equal to 5
  Expected: is <5>
    but: was <4>
</code>

lessThan

Given the following example:

<code lang="java">
  public void testAssert() {
    int n = 4;
    assertThat("Assert that n is less than 3", n, Matchers
        .lessThan(3));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert that n is less than 3
  Expected: is a value less than <3>
    but: <4> was greater than <3>
</code>

greaterThan

Given the following example:

<code lang="java">
  public void testAssert() {
    int n = 4;
    assertThat("Assert that n is greater than 6", n, Matchers
        .greaterThan(6));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert that n is greater than 6
  Expected: is a value greater than <6>
    but: <4> was less than <6>
</code>

closeTo

Given the following example:

<code lang="java">
  public void testAssert() {
    double a = 5.0;
    assertThat("Assert that a is close to 7.0", a, Matchers
        .closeTo(7.0, 1.0));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert that a is close to 7.0
  Expected: is a numeric value within <1.0> of <7.0>
    but: <5.0> differed by <1.0>
</code>

Leave a Reply

Your email address will not be published. Required fields are marked *