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

        Jerry Duffy created issue -
        Jerry Duffy made changes -
        Field Original Value New Value
        Status Open [ 1 ] Edit [ 10000 ]
        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.
        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.
        Jerry Duffy made changes -
        Status Edit [ 10000 ] Open [ 1 ]
        Jerry Duffy made changes -
        Status Open [ 1 ] Edit [ 10000 ]
        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.
        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.
        Jerry Duffy made changes -
        Status Edit [ 10000 ] Open [ 1 ]
        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.
        Jeff Scott Brown made changes -
        Fix Version/s 2.0.4 [ 13099 ]
        Fix Version/s 2.1 [ 12801 ]
        Priority Major [ 3 ] Blocker [ 1 ]
        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
        Jeff Scott Brown made changes -
        Assignee Jeff Brown [ brownj ]
        Jeff Scott Brown made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        Jeff Brown 26/Apr/2012 8:44 AM PDT
        View full commit
        GRAILS-9057 - fix assertion failures

        import groovy.util.GroovyTestCase.* instead of org.junit.Assert.* so methods provided by GroovyTestCase get precedence over those provided by Assert.
        Jeff Brown 30/Apr/2012 8:36 AM PDT
        View full commit
        GRAILS-9057 - fix assertion failures

        import groovy.util.GroovyTestCase.* instead of org.junit.Assert.* so methods provided by GroovyTestCase get precedence over those provided by Assert.
        Jeff Scott Brown made changes -
        Status In Progress [ 3 ] Closed [ 6 ]
        Resolution Fixed [ 1 ]

          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