Last active
October 10, 2015 19:58
-
-
Save arturoherrero/3742619 to your computer and use it in GitHub Desktop.
Testing Utils. Extend the default assertion methods inherited from the JUnit framework's TestCase and GroovyTestCase class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='org.hamcrest', module='hamcrest-all', version='1.3') | |
@Grab(group='org.spockframework', module='spock-core', version='0.7-groovy-2.0') | |
import static org.hamcrest.Matchers.not | |
import org.hamcrest.BaseMatcher | |
import org.hamcrest.Description | |
import spock.lang.Specification | |
class DateEqualsHamcrestSpec extends Specification { | |
def "test date equals"() { | |
setup: | |
def today = new Date() | |
def tomorrow = today + 1 | |
expect: | |
today dateEquals(today) | |
today not(dateEquals(tomorrow)) | |
} | |
def dateEquals(Date actual) { | |
[ | |
matches: { Date expected -> expected.format('dd/MM/yyyy') == actual.format('dd/MM/yyyy') }, | |
describeTo: { Description description -> description.appendText("equivalent dates according to the day, month and year") } | |
] as BaseMatcher | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.util.GroovyTestCase | |
import org.junit.rules.ExpectedException | |
/** | |
* Testing util class that provides a number of helper methods. | |
*/ | |
class TestingUtils extends GroovyTestCase { | |
/** | |
* Asserts that the maps are equivalent and contain the same values. | |
*/ | |
static assertMapEquals(Map expected, Map actual) { | |
expected.each { k, v -> assertEquals v, actual[k] } | |
} | |
/** | |
* Asserts that the dates are equivalent according to the day, month and year. | |
*/ | |
static assertDateEquals(Date expected, Date actual) { | |
assertEquals expected.format('dd/MM/yyyy'), actual.format('dd/MM/yyyy') | |
} | |
/** | |
* Asserts that the objects are not equals. | |
*/ | |
static assertNotEquals(Object expected, Object actual) { | |
if (expected.equals(actual)) throw ExpectedException('Objects are not equals') | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static TestingUtils.* | |
import groovy.util.GroovyTestCase | |
class TestingUtilsTests extends GroovyTestCase { | |
protected void setUp() { | |
super.setUp() | |
} | |
protected void tearDown() { | |
super.tearDown() | |
} | |
void testAssertMapEquals() { | |
def map = [a: 1, b: 2] | |
def otherMap = [a: 1, c: 3] | |
assertMapEquals(map, map) | |
shouldFail { assertMapEquals(map, otherMap) } | |
} | |
void testAssertDateEquals() { | |
def date = new Date() | |
def otherDate = new Date().plus(1) | |
assertDateEquals(date, date) | |
shouldFail { assertDateEquals(date, otherDate) } | |
} | |
void testAssertNotEquals() { | |
assertNotEquals(1, 2) | |
shouldFail { assertNotEquals(1, 1) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment