groovy
  1. groovy
  2. GROOVY-3134

Cannot override private static method using EMC

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.5.6, 1.7.0
    • Fix Version/s: 3.0
    • Component/s: groovy-runtime
    • Labels:
      None
    • Environment:
      Windows XP, Eclipse Ganymede, Groovy Eclipse plugin using Groovy 1.5.6
    • Number of attachments :
      0

      Description

      Here's an example of a simple test, in which the return type of a Private Static Method within a Class is needed to return a String different from what it would normally return:

      void testCheckSunrise(){
          WeatherMonitor.metaClass.static.findSunriseTime = {-> '5:57 AM'}
          WeatherMonitor testSubject = new WeatherMonitor()	
          assertEquals('5:57 AM', testSubject.sunrise)
      }
      

      And here is the Class itself:

      public class WeatherMonitor {
          def getSunrise(){
              findSunriseTime()
          }
      
          private static findSunriseTime(){
              '7:15 AM'
          }
      }
      

      The result of the test execution is always '7:15 AM' and so the test will always fail. This would be a great help in Unit Testing techniques, especially Test Driven Development in Groovy.

      It would be great to see Groovy have this capability.

        Activity

        Hide
        Paul King added a comment -

        add code tags

        Show
        Paul King added a comment - add code tags
        Paul King made changes -
        Field Original Value New Value
        Description Here's an example of a simple test, in which the return type of a Private Static Method within a Class is needed to return a String different from what it would normally return:

        void testCheckSunrise(){
        WeatherMonitor.metaClass.'static'.findSunriseTime = {-> '5:57 AM'}
        WeatherMonitor testSubject = new WeatherMonitor()
        assertEquals('5:57 AM', testSubject.getSunrise())
        }

        And here is the Class itself:

        public class WeatherMonitor{
               
              def getSunrise(){
        findSunriseTime()
        }

        private static findSunriseTime(){
        '7:15 AM'
        }
        }

        The result of the test execution is always '7:15 AM' and so the test will always fail. This would be a great help in Unit Testing techniques, especially Test Driven Development in Groovy.

        It would be great to see Groovy have this capability.


        Here's an example of a simple test, in which the return type of a Private Static Method within a Class is needed to return a String different from what it would normally return:
        {code}
        void testCheckSunrise(){
            WeatherMonitor.metaClass.'static'.findSunriseTime = {-> '5:57 AM'}
            WeatherMonitor testSubject = new WeatherMonitor()
            assertEquals('5:57 AM', testSubject.sunrise)
        }
        {code}
        And here is the Class itself:
        {code}
        public class WeatherMonitor {
            def getSunrise(){
                findSunriseTime()
            }

            private static findSunriseTime(){
                '7:15 AM'
            }
        }
        {code}
        The result of the test execution is always '7:15 AM' and so the test will always fail. This would be a great help in Unit Testing techniques, especially Test Driven Development in Groovy.

        It would be great to see Groovy have this capability.


        Paul King made changes -
        Description Here's an example of a simple test, in which the return type of a Private Static Method within a Class is needed to return a String different from what it would normally return:
        {code}
        void testCheckSunrise(){
            WeatherMonitor.metaClass.'static'.findSunriseTime = {-> '5:57 AM'}
            WeatherMonitor testSubject = new WeatherMonitor()
            assertEquals('5:57 AM', testSubject.sunrise)
        }
        {code}
        And here is the Class itself:
        {code}
        public class WeatherMonitor {
            def getSunrise(){
                findSunriseTime()
            }

            private static findSunriseTime(){
                '7:15 AM'
            }
        }
        {code}
        The result of the test execution is always '7:15 AM' and so the test will always fail. This would be a great help in Unit Testing techniques, especially Test Driven Development in Groovy.

        It would be great to see Groovy have this capability.


        Here's an example of a simple test, in which the return type of a Private Static Method within a Class is needed to return a String different from what it would normally return:
        {code}
        void testCheckSunrise(){
            WeatherMonitor.metaClass.static.findSunriseTime = {-> '5:57 AM'}
            WeatherMonitor testSubject = new WeatherMonitor()
            assertEquals('5:57 AM', testSubject.sunrise)
        }
        {code}
        And here is the Class itself:
        {code}
        public class WeatherMonitor {
            def getSunrise(){
                findSunriseTime()
            }

            private static findSunriseTime(){
                '7:15 AM'
            }
        }
        {code}
        The result of the test execution is always '7:15 AM' and so the test will always fail. This would be a great help in Unit Testing techniques, especially Test Driven Development in Groovy.

        It would be great to see Groovy have this capability.


        Hide
        Paul King added a comment -

        Note: removing the 'private' modifier on the static method allows the EMC change to be seen.

        Show
        Paul King added a comment - Note: removing the 'private' modifier on the static method allows the EMC change to be seen.
        Hide
        Paul King added a comment -

        I wonder whether this belongs under a different component? It is more related to EMC than MockFor/StubFor.

        Show
        Paul King added a comment - I wonder whether this belongs under a different component? It is more related to EMC than MockFor/StubFor.
        Hide
        blackdrag blackdrag added a comment -

        could you use MockFor/StubFor to achieve what the issue opener wants to do?

        Show
        blackdrag blackdrag added a comment - could you use MockFor/StubFor to achieve what the issue opener wants to do?
        Hide
        Paul King added a comment -

        Well certainly you could mock out the call to getSunrise and get it to return the required String but you could do that with EMC too. We would need to support half-mocks to get MockFor to mock out just the private method but leave the public one as is.

        Show
        Paul King added a comment - Well certainly you could mock out the call to getSunrise and get it to return the required String but you could do that with EMC too. We would need to support half-mocks to get MockFor to mock out just the private method but leave the public one as is.
        Hide
        blackdrag blackdrag added a comment -

        I made some changes to the issue to reflect, that it is about EMC and not really about testing.

        Show
        blackdrag blackdrag added a comment - I made some changes to the issue to reflect, that it is about EMC and not really about testing.
        blackdrag blackdrag made changes -
        Component/s mocks and stubs [ 13113 ]
        Fix Version/s 1.8-beta-x [ 15750 ]
        Issue Type Test [ 6 ] Bug [ 1 ]
        Affects Version/s 1.7.0 [ 14665 ]
        Summary Cannot override private static method with Groovy's MetaClass Cannot override private static method using EMC
        blackdrag blackdrag made changes -
        Fix Version/s 3.0 [ 13489 ]
        Fix Version/s 1.8.x [ 15750 ]
        Hide
        blackdrag blackdrag added a comment -

        I think we also have to discuss if it should be actually possible to shadow private methods like this

        Show
        blackdrag blackdrag added a comment - I think we also have to discuss if it should be actually possible to shadow private methods like this
        blackdrag blackdrag made changes -
        Component/s groovy-runtime [ 16250 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Michael Nyika
          • Votes:
            5 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated: