Grails Unit Tests: Why does this statement fail?

Posted by leeand00 on Stack Overflow See other posts from Stack Overflow or by leeand00
Published on 2010-03-30T02:17:21Z Indexed on 2010/03/30 2:23 UTC
Read the original article Hit count: 402

Filed under:
|
|
|

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)

© Stack Overflow or respective owner

Related posts about grails

Related posts about junit