Question

I've developed in Java in the past, and now I'm trying to learn Grails/Groovy using this slightly dated tutorial.

import grails.test.*

class DateTagLibTests extends TagLibUnitTestCase {

    def dateTagLib

    protected void setUp() {
        super.setUp()
        dateTagLib = new DateTagLib()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testThisYear() {
        String expected = Calendar.getInstance().get(Calendar.YEAR) 

        // NOTE: This statement fails
        assertEquals("the years dont match and I dont know why.", expected, dateTagLib.thisYear())

    }
}

DateTagLibTests.groovy
(Note: this TagLibUnitTestCase is for Grails 1.2.1 and not the version used in the tutorial)

For some reason the above test fails with:

expected:<2010> but was:<2010>

I've tried replacing the test above with the following alternate version of the test, and the test passes just fine:

void testThisYear() {
    String expected = Calendar.getInstance().get(Calendar.YEAR)
    String actual = dateTagLib.thisYear()

    // NOTE: The following two assertions work:
    assertEquals("the years don\'t match", expected, actual)
    assertTrue("the years don\'t match", expected.equals(actual))
}

These two versions of the test are basically the same thing right?

Unless there's something new in Grails 1.2.1 or Groovy that I'm not understanding. They should be of the same type because the values are both the value returned by Calendar.getInstance().get(Calendar.YEAR)

Was it helpful?

Solution

The object returned from dateTagLib.thisYear() must not be a string.

Try

  assertEquals("the years dont match and I dont know why.", expected, dateTagLib.thisYear().toString())

In your working example, Groovy is converting .thisYear() to a String for you.

Print out dateTagLib.thisYear().class to be sure.

cheers

Lee

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow