This was taken nearly verbatim from IBM's Mastering Grails series.
DateTagLib.groovy:
class DateTagLib {
def thisYear = {
out << Calendar.getInstance().get(Calendar.YEAR)
}
}
DateTagLibTests.groovy:
class DateTagLibTests extends TagLibUnitTestCase {
def dateTagLib
protected void setUp() {
super.setUp()
dateTagLib = new DateTagLib()
}
void testThisYear() {
String expected = Calendar.getInstance().get(Calendar.YEAR)
assertEquals("years do NOT match", expected, dateTagLib.thisYear())
}
protected void tearDown() {
super.tearDown()
}
}
grails test-app DateTagLib
output:
-------------------------------------------------------
Running 1 unit test...
Running test DateTagLibTests...
testThisYear...FAILED
Tests Completed in 359ms ...
-------------------------------------------------------
Tests passed: 0
Tests failed: 1
-------------------------------------------------------
I tried matching the types (int/long/String), but I'm still banging my head against the wall.
This test also fails:
void testThisYear() {
long expected = Calendar.getInstance().get(Calendar.YEAR)
assertEquals("years do NOT match", expected, (long) dateTagLib.thisYear())
}