Grails
  1. Grails
  2. GRAILS-9057

assertEquals() fails when comparing a java.lang.String and a org.codehaus.groovy.runtime.GStringImp with identical values

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.0.1, 2.0.3
    • Fix Version/s: 2.0.4, 2.1-RC1
    • Component/s: Testing
    • Labels:
    • Environment:
      Mac OS X 10.7.3, Java: 1.6.0_29 from Apple
      Also duplicated on Windows 7

      Description

      The following unit test class will pass successfully when run as a Groovy Script, outside of the grails environment:

      class SampleTests extends GroovyTestCase {

      void testSomething()

      { def constString = "world"; def javaString = 'hello ' + constString; def gString = "hello $constString"; assertEquals(javaString, gString); }

      }

      new SampleTests().testSomething()
      //End Script

      A similar unit test, when run in the grails environment (grails -test-app) will fail with the following AssertionError:
      java.lang.AssertionError: expected: java.lang.String<hello world> but was: org.codehaus.groovy.runtime.GStringImpl<hello world>

      This same scenario, when run in Grails 1.3.7, passes as expected. I've attached a zip file that contains a vanilla grails project, with a single groovy class: test/unit/SampleTests.groovy

      Note that if the statement:
      assert javaString == gString
      is used, the test passes in grails.

        Activity

        Hide
        Jeff Scott Brown added a comment -

        We may be dispatching to the wrong method.

        This test fails:

        test/unit/SampleTests.groovy
        
        class SampleTests extends GroovyTestCase {
            void testSomething() {
                def constString = "world"
                def javaString = 'hello ' + constString
                def gString = "hello $constString"
        
                assertEquals(javaString, gString)
            }
        }
        

        This test passes:

        test/unit/SampleTests.groovy
        
        class SampleTests extends GroovyTestCase {
            void testSomething() {
                def constString = "world"
                def javaString = 'hello ' + constString
                def gString = "hello $constString"
        
                GroovyTestCase.assertEquals(javaString, gString)
            }
        }
        

        The first example is dispatching to org.junit.Assert.assertEquals.

        Show
        Jeff Scott Brown added a comment - We may be dispatching to the wrong method. This test fails: test/unit/SampleTests.groovy class SampleTests extends GroovyTestCase { void testSomething() { def constString = "world" def javaString = 'hello ' + constString def gString = "hello $constString" assertEquals(javaString, gString) } } This test passes: test/unit/SampleTests.groovy class SampleTests extends GroovyTestCase { void testSomething() { def constString = "world" def javaString = 'hello ' + constString def gString = "hello $constString" GroovyTestCase.assertEquals(javaString, gString) } } The first example is dispatching to org.junit.Assert.assertEquals.
        Hide
        Jeff Scott Brown added a comment -

        I believe this is because of the "import static org.junit.Assert.*" that is added by GrailsTestCompiler.

        Show
        Jeff Scott Brown added a comment - I believe this is because of the "import static org.junit.Assert.*" that is added by GrailsTestCompiler.
        Show
        Jeff Scott Brown added a comment - See http://jira.codehaus.org/browse/GROOVY-5423

          People

          • Assignee:
            Jeff Scott Brown
            Reporter:
            Jerry Duffy
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development