How to avoid casting arguments in Spock

99封情书 提交于 2020-01-24 23:31:23

问题


I want to get a List from repository and assert its contents.

In following code I get a warning that states that Object cannot be assigned to List

Is there any way to add better argument to handle such case?

myDomainObjectRepository.save(_) >> { arguments ->
   final List<MyDomainObject> myDomainObjects = arguments[0]
   assert myDomainObjects == [new MyDomainObject(someId, someData)]
}

回答1:


To elaborate on Opals answer: There are two parts and a footnote in the docs that are relevant here:

If the closure declares a single untyped parameter, it gets passed the method’s argument list:

And

In most cases it would be more convenient to have direct access to the method’s arguments. If the closure declares more than one parameter or a single typed parameter, method arguments will be mapped one-by-one to closure parameters[footnote]:

Footnote:

The destructuring semantics for closure arguments come straight from Groovy.

The problem is that you have a single argument List, and since generics are erased groovy can't decide that you actually want to unwrap the list.

So a single non-List argument works fine:

myDomainObjectRepository.save(_) >> { MyDomainObject myDomainObject ->
   assert myDomainObject == new MyDomainObject(someId, someData)
}

or a List argument combined with a second, e.g., save(List domain, boolean flush)

myDomainObjectRepository.save(_, _) >> { List<MyDomainObject> myDomainObjects, boolean flush -> 
   assert myDomainObjects == [new MyDomainObject(someId, someData)]
}

So the docs are a little bit misleading about this edge case. I'm afraid that you are stuck with casting for this case.


Edit: You should be able to get rid of the IDE warnings if you do this.

myDomainObjectRepository.save(_) >> { List<List<MyDomainObject>> arguments ->
   List<MyDomainObject> myDomainObjects = arguments[0]
   assert myDomainObjects == [new MyDomainObject(someId, someData)]
}



回答2:


The docs seems to be precise:

If the closure declares a single untyped parameter, it gets passed the method’s argument list

However I've just changed my spec that uses rightShift + arguments to accept a single type argument and it did work. Try it out.



来源:https://stackoverflow.com/questions/46778368/how-to-avoid-casting-arguments-in-spock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!