spock icon indicating copy to clipboard operation
spock copied to clipboard

Mock a Map delegate reports MissingMethodException

Open pditommaso opened this issue 5 years ago • 1 comments

Issue description

When mocking an object implementing a Map interface with a delegate implementation an invalid MissingMethodException is reported accessing mock properties.

For example:

class MyMap implements Map<String,Object> {
    @Delegate
    private Map<String,Object> target

    MyMap(Map entries) {
        this.target = entries
    }
} 

The following test fails:

  def 'should mock map delegate' () {
      given:
      def ctx = Mock(MyMap)

      expect:
      ctx.foo == null
  }

Error message:

Condition failed with Exception:

ctx.foo == null
|   |
|   groovy.lang.MissingMethodException: No signature of method: spock.mock.issue.MyMap.getFoo() is applicable for argument types: () values: []
|   Possible solutions: get(java.lang.Object), getAt(java.lang.String), getAt(java.lang.Object), get(java.lang.Object, java.lang.Object), getClass(), notify()
|   	at spock.mock.issue.MyMapTest.should mock map delegate(MyMapTest.groovy:20)
spock.mock.issue.MyMap$$EnhancerByCGLIB$$460469d@1ae8bcbc (renderer threw NullPointerException)

NOTE: the same is OK with Spock 1.2 (and Groovy 2.5)

How to reproduce

Clone this test case and run ./gradlew test

Java/JDK

$ java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

Groovy version

    compile "org.codehaus.groovy:groovy:3.0.3"
    testCompile ("org.spockframework:spock-core:2.0-M2-groovy-3.0") 
    testCompile ('org.spockframework:spock-junit4:2.0-M2-groovy-3.0') 

Build tool version

Gradle

$ ./gradlew --version

------------------------------------------------------------
Gradle 5.5.1
------------------------------------------------------------

Build time:   2019-07-10 20:38:12 UTC
Revision:     3245f748c7061472da4dc184991919810f7935a5

Kotlin:       1.3.31
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.14 compiled on March 12 2019
JVM:          1.8.0_161 (Oracle Corporation 25.161-b12)
OS:           Mac OS X 10.14.4 x86_64

pditommaso avatar Apr 18 '20 17:04 pditommaso

Whoever may interest, solved using GroovyMock instead of Mock.

pditommaso avatar Aug 01 '20 13:08 pditommaso

#1729 would fix this issue

AndreasTu avatar Aug 02 '23 11:08 AndreasTu

Was fixed by https://github.com/spockframework/spock/pull/1729

AndreasTu avatar Sep 15 '23 17:09 AndreasTu