Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spock framework: matching wildcard arguments

I am trying to write a Spock specification, where I want to verify a method that is being called with three arguments. I don't care about the first two at all, any instance of the argument types would do. I'm trying to use Spock wildcard argument matching but keep running into issues. My verification looks like this:

    when:
    packageUploadController.handleUpload(httpRequest)

    then: "the value of the 'dest' parameter is passed on to saveservice"
    saveService.saveImportPackage(_ as UploadedPackage, _ as PackageImportResponse.Builder)

Here saveService is a mock and UploadedPackage and PackageImportResponse.Builder are the expected arguments; I'm trying to get to a working test first before changing the code that calls saveService.

As far as I can see I'm doing this as documented, however the test fails with the following message:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: 
Cannot cast object '_' with class 'org.spockframework.lang.Wildcard' to 
class 'UploadedPackage' due to:
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: 
UploadedPackage(org.spockframework.lang.SpreadWildcard)

I don't understand this message as it seems to indicate that my production code needs to provide a constructor taking a SpreadWildCard as an argument. What am I doing wrong here?

I also tried leaving the types out and just match on (_, _), however this also fails:

org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: 
SaveService$$EnhancerByCGLIB$$67b7324.saveImportPackage() is applicable for argument types: (org.spockframework.lang.Wildcard, org.spockframework.lang.Wildcard) values: [[*_], [*_]]

Here also I think I'm doing it as per the documentation, and don't understand why this fails (I added the types later to try and get rid of this error).

like image 535
Ton van Bart Avatar asked May 10 '16 14:05

Ton van Bart


1 Answers

Put 1 * and then saveService.saveImportPackage(_ as UploadedPackage, _ as PackageImportResponse.Builder) this seems to work on a comparable case. Not completely clear why

 then: "the value of the 'dest' parameter is passed on to saveservice"
 1 * saveService.saveImportPackage(_ as UploadedPackage, _ as PackageImportResponse.Builder)
like image 173
devnoo Avatar answered Oct 18 '22 14:10

devnoo