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

Stubbing static methods don't work on instaces

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.4.1, 2.5.0-beta-1, 3.0.4
    • None
    • mocks and stubs
    • Winows 8.1, JDK 1.8

    Description

      When replaced static method is called from an instance of a class, it still hits original method. Here is code example that fails:

      StaticStubFails.java
      class Foo {
        static String greet() {
          'Hello from Foo'
         }
      }
      
      foo = new Foo()
      assert 'Hello from Foo' == foo.greet()
      Foo.metaClass.static.greet = { 'Hello from Stub' }
      assert 'Hello from Stub' == Foo.greet()
      
      //uncomment line below and it will pass
      //foo.metaClass.static.greet = { 'Hello from Stub' }
      
      assert 'Hello from Stub' == foo.greet()
      println 'All ok'
      
      Output
      assert 'Hello from Stub' == foo.greet()
                               |  |   |
                               |  |   Hello from Foo
                               |  Foo@5e82df6a
                               false
      

      Attachments

        1. StaticStubFails.groovy
          0.4 kB
          Markus Geiss

        Activity

          People

            Unassigned Unassigned
            mage Markus Geiss
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: