Skip to content

Passing GString to groovy-mocked method taking String argument yields IllegalArgumentException #1216

@kriegaex

Description

@kriegaex

Issue description

When creating a GroovyMock for a class with a method someMethod(String arg) and then passing a GString to it during a test, e.g. someMethod("foo ${1 + 1}"), it will yield java.lang.IllegalArgumentException: argument type mismatch.

For me this looks like a bug.

How to reproduce

package de.scrum_master.stackoverflow.q63797837

import spock.lang.Specification

class FooSpec extends Specification {
  def 'pass String to groovyMock mocked method taking a String argument'() {
    given:
    TestClass testClass = new TestClass()
    SomeClass someClass = GroovyMock()  // does not work, but Mock() does
    testClass.someClass = someClass

    when:
    testClass.callMethod()

    then:
    1 * someClass.someMethod(_)
  }

  static class SomeClass {
    String someMethod(String arg) {  // changing parameter type to GString works
      return arg
    }
  }

  static class TestClass {
    SomeClass someClass

    String callMethod() {
      someClass.someMethod("foo ${1 + 1}")
    }
  }

}

The (unfiltered) stack trace looks like this:

java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:104)
    at org.spockframework.mock.runtime.GroovyMockMetaClass.doInvokeMethod(GroovyMockMetaClass.java:83)
    at org.spockframework.mock.runtime.GroovyMockMetaClass.invokeMethod(GroovyMockMetaClass.java:39)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:41)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
    at de.scrum_master.stackoverflow.q63797837.TestClass.callMethod(FooSpec.groovy:15)
    at de.scrum_master.stackoverflow.q63797837.TestClass$callMethod.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:119)
    at de.scrum_master.stackoverflow.q63797837.FooSpec.$spock_feature_0_0(FooSpec.groovy:28)
    ... 34 more

Link to a gist or similar (optional)

Origin: https://stackoverflow.com/q/63797837/1082681

Spock versions

Happens both in 1.3-groovy-2.5 and 2.0-M3-groovy-3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions