Skip to content

Instantly share code, notes, and snippets.

@jpertino
Created January 8, 2011 02:09

Revisions

  1. jpertino created this gist Jan 8, 2011.
    58 changes: 58 additions & 0 deletions AssertAndAssumeTest.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@

    import static org.hamcrest.CoreMatchers.is

    import static org.junit.Assert.assertArrayEquals
    import static org.junit.Assert.assertEquals
    import static org.junit.Assert.assertSame
    import static org.junit.Assert.assertNot
    import static org.junit.Assert.assertNull
    import static org.junit.Assert.assertNotNull
    import static org.junit.Assume.assumeNoException
    import static org.junit.Assume.assumeNotNull
    import static org.junit.Assume.assumeThat
    import static org.junit.Assume.assumeTrue

    import org.junit.Test

    class AssertAndAssumeTest {

    @Test void assumeTrueExample() {
    assert "Test reaches this point"
    assumeTrue "these strings" == "are obviously different"
    assert false, "Test should have stopped on assumeTrue"
    }

    @Test void assumeNotNullExample() {
    assert "Test reaches this point"
    assumeNotNull 1, 2, null, "one of the values is null"
    assert false, "Test should have stopped on assumeNotNull"
    }

    @Test void assumeNoExceptionExample() {
    assert "Test reaches this point"
    try {
    1 / 0 // force exception
    } catch (e) {
    assumeNoException e
    }
    assert false, "Test should have stopped on assumeNoException"
    }

    @Test void assumeThatExample() {
    assert "Test reaches this point"
    assumeThat "this string", is("this other string")
    assert false, "Test should have stopped on assumeThat"
    }

    @Test void assertArrayEqualsExample() {
    assertArrayEquals "int arrays should be equal",
    [1, 2] as int[], [1, 2] as int[]
    assertArrayEquals "Integer(Object) should be equal",
    [1, 2] as Integer[], [1, 2] as Integer[]
    assertArrayEquals "double arrays should be equal but accept a margin of error",
    [1.1D, 1.2D] as double[], [1.0D, 1.3D] as double[], 0.11D
    assertArrayEquals "String(Object) arrays should be equal",
    ["1", "2"] as String[], ["1", "2"] as String[]
    }

    }
    116 changes: 116 additions & 0 deletions HamcrestMatchersTest.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@

    import static org.junit.Assert.assertThat

    import static org.hamcrest.CoreMatchers.notNullValue
    import static org.hamcrest.CoreMatchers.nullValue

    import static org.hamcrest.CoreMatchers.is
    import static org.hamcrest.CoreMatchers.not

    import static org.hamcrest.CoreMatchers.equalTo
    import static org.hamcrest.CoreMatchers.instanceOf
    import static org.hamcrest.CoreMatchers.sameInstance

    import static org.hamcrest.CoreMatchers.any
    import static org.hamcrest.CoreMatchers.anything

    import static org.hamcrest.CoreMatchers.allOf
    import static org.hamcrest.CoreMatchers.anyOf

    import org.junit.Test

    class HamcrestMatchersTest {

    @Test void nullValueMatcherExample() {
    String nullReference = null
    assertThat "nullValue() requires a null",
    nullReference, nullValue()
    assertThat "nullValue() could accept a class for type inference",
    nullReference, nullValue(String)
    assertThat "nullValue() with class is useless in groovy afaik",
    nullReference, nullValue(String)
    }

    @Test void notNullValueMatcherExample() {
    String notNullReference = "=D"
    assertThat "notNullValue() requires a non null",
    notNullReference, notNullValue()
    assertThat "notNullValue() could accept a class for type inference",
    notNullReference, notNullValue(Integer)
    assertThat "notNullValue() with class is useless in groovy afaik",
    notNullReference, notNullValue(Integer)
    }

    @Test void isMatcherExample() {
    assertThat "is() wraps another Matcher", "this", is(equalTo("this"))
    assertThat "is() wraps another Matcher", "this", is(not("that"))
    assertThat "is() automaticaly wraps an equalTo()", "this", is("this")
    assertThat "is() automaticaly wraps an instanceOf()", "this", is(String)
    }

    @Test void notMatcherExample() {
    assertThat "not() negates another Matcher", "this", not(equalTo("that"))
    assertThat "not() negates another Matcher", "this", not(is("that"))
    assertThat "not() automaticaly negates an equalTo()", "this", not("that")
    assertThat "not() automaticaly negates an instanceOf()", "this", not(Long)
    }

    @Test void instanceOfMatcherExample() {
    assertThat "instanceOf() matches instances of a given class",
    "this", instanceOf(String)
    assertThat "instanceOf() matches instances of a given class",
    "this", not(instanceOf(Integer))
    }

    @Test void equalToMatcherExample() {
    assertThat "equalTo() matches objects using equals()",
    "this", equalTo("this")
    assertThat "equalTo() does not compare objects using ==",
    "this", equalTo("th" + "is")
    }

    @Test void sameInstanceMatcherExample() {
    assertThat "sameInstance() compares objects using ==",
    "this", sameInstance("this")
    assertThat "sameInstance() does not compare objects using equals()",
    "this", not(sameInstance("th" + "is"))
    }

    @Test void anythingMatcherExample() {
    assertThat "anything() matches nulls", null, is(anything())
    assertThat "anything() matches objects", "this", is(anything())
    assertThat "anything() matches primitives", 8D, is(anything())
    assertThat "anything() accepts a description", null, is(anything("really"))
    }

    @Test void anyMatcherExample() {
    assertThat "any() is like anything() but uses a class for type inference",
    (String) null, is(any(String))
    assertThat "any() is useless in groovy afaik",
    (Long) null, is(any(String))
    }

    @Test void allOfMatcherExample() {
    assertThat "allOf() requires all matchers to match the input",
    ["one", "two", "three"],
    allOf(
    anything(),
    any(List),
    notNullValue(),
    notNullValue(List),
    instanceOf(List),
    equalTo(["one", "two", "three"]))
    }

    @Test void anyOfMatcherExample() {
    assertThat "anyOf() requires only one matcher to match the input",
    ["one", "two", "three"],
    anyOf(
    sameInstance(["obviously not"]),
    equalTo(["again false"]),
    instanceOf(Void),
    nullValue(List),
    anything())
    }

    }