Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10717

Map to type coercion not working on abstract classes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.11, 4.0.2
    • 2.5.19, 4.0.6, 3.0.14
    • None
    • None

    Description

      I sometimes use maps as mock objects for test cases. One test case started failing with groovy 3.0.11 and 4.0.2 where a map was being used as a mock for an abstract class. Here is a simplified version of what is now failing. Note that it does not fail if A is not abstract:

      abstract class A {
          String b
      }
      
      def a = [getB: { 'string b' }] as A
      assert 'string b' ==  a.b

       

      I also noticed that I don't get an UnsupportedOperationException when calling functions that were not implemented in the map as described in https://groovy-lang.org/semantics.html#_map_to_type_coercion. MissingPropertyException seems to be working, though I can't seem to find a way to get MissingMethodException.

      abstract class A {
          String b
          String c
      }
      
      def a = [getB: { 'string b' }, getD: { 'string d' }] as A
      assert null != a.c // Should get UnsupportedOperationException (doesn't work)
      assert null != a.d // Should get MissingPropertyException (works)
      assert null != a.getD() // Should get MissingMethodException (doesn't work) 
      

       

      Possibly related to GROOVY-8243?

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              chessami92 Josh DeWitt
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: