Forgus / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

Using Mock() in a static method gives unhelpful error message #270

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Spock 0.6:

 41         static Exchange mockExchange(body, Map headers) {
 42                 Exchange x = Mock()
 43                 def out = Mock(Message)
 44                 if(body) out.body >> body
 45                 out.headers >> headers
 46                 x.out >> out
 47                 return x
 48         }

Gives the following exception:

| Error Error executing script TestApp: (class:
frontlinesms2/WebConnectionServiceSpec, method: mockExchange
signature: (Ljava/lang/Object;Ljava/util/Map;)Lorg/apache/camel/Exchange;)
Incompatible type for getting or setting field (NOTE: Stack trace has
been filtered. Use --verbose to see entire trace.)
java.lang.VerifyError: (class: frontlinesms2/WebConnectionServiceSpec,
method: mockExchange signature:
(Ljava/lang/Object;Ljava/util/Map;)Lorg/apache/camel/Exchange;)
Incompatible type for getting or setting field
        at org.spockframework.runtime.SpecUtil.getFeatureCount(SpecUtil.java:85)
        at org.spockframework.runtime.SpecUtil$getFeatureCount.call(Unknown Source)
        at grails.plugin.spock.test.GrailsSpecTestType$_doPrepare_closure2.doCall(GrailsSpecTestType.groovy:61)
        at grails.plugin.spock.test.GrailsSpecTestType.doPrepare(GrailsSpecTestType.groovy:61)
        at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:282)
        at _GrailsTest_groovy$_run_closure4.call(_GrailsTest_groovy)
        at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:248)
        at _GrailsTest_groovy$_run_closure1_closure21.doCall(_GrailsTest_groovy:195)
        at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:184)
        at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
        at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
        at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy:185)
        at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy)
        at java_util_concurrent_Callable$call.call(Unknown Source)
        at org.codehaus.gant.GantBinding.withTargetEvent(GantBinding.groovy:90)
        at org.codehaus.gant.GantBinding.this$4$withTargetEvent(GantBinding.groovy)
        at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy:185)
        at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy)
        at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
        at TestApp$_run_closure1.doCall(TestApp.groovy:82)
        at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
        at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
        at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy:185)
        at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy)
        at java_util_concurrent_Callable$call.call(Unknown Source)
        at org.codehaus.gant.GantBinding.withTargetEvent(GantBinding.groovy:90)
        at org.codehaus.gant.GantBinding.this$4$withTargetEvent(GantBinding.groovy)
        at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy:185)
        at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy)
        at java_util_concurrent_Callable$call.call(Unknown Source)
        at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
        at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
        at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
        at java_util_concurrent_Callable$call.call(Unknown Source)
        at gant.Gant.withBuildListeners(Gant.groovy:427)
        at gant.Gant.this$2$withBuildListeners(Gant.groovy)
        at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
        at gant.Gant.dispatch(Gant.groovy:415)
        at gant.Gant.this$2$dispatch(Gant.groovy)
        at gant.Gant.invokeMethod(Gant.groovy)
        at gant.Gant.executeTargets(Gant.groovy:591)
        at gant.Gant.executeTargets(Gant.groovy:590)

Removing `static` seems to fix it...

Would be helpful to detect calls to Mock() from static context and give an 
error such as "Mock() cannot be called from a static context."

Original issue reported on code.google.com by alexande...@gmail.com on 1 Oct 2012 at 12:28

GoogleCodeExporter commented 8 years ago

Original comment by pnied...@gmail.com on 4 Oct 2012 at 6:52

GoogleCodeExporter commented 8 years ago
We haven't been able to reproduce this in master. What we see is the expected 
behavior, namely a compile-time error saying that mock objects can't be created 
in static context. Maybe this used to be a problem in 0.6.

If you run into this again, and can provide a reproducible example, please open 
another issue.

Original comment by pnied...@gmail.com on 17 Mar 2013 at 10:45