Reputation: 75
Trying to figure out why Spock doesn't seem to recognize a method call (of a Mocked object) as an invocation. Looked at the docs (http://spockframework.org/spock/docs/1.1-rc-3/all_in_one.html#_mocking) and couldn't figure it out.
Here's a dumbed down version of the code:
class VmExportTaskSplitter implements TaskSplitter<Export> {
@Inject
AssetServiceClient assetServiceClient
@Override
int splitAndSend(Export export) {
Map batch = [:]
Map tags = [:]
if (true) {
println('test')
batch = assetServiceClient.getAssetIdBatch(export.containerUuid,
export.userUuid, (String) batch.scrollId, tags)
print('batch: ')
println(batch)
}
return 1
}
}
And now the test:
class VmExportTaskSplitterSpecification extends Specification{
def "tags should be parsed correctly"(){
setup:
Export export = new Export(containerUuid: "000", userUuid: "000", chunkSize: 10)
AssetServiceClient client = Mock(AssetServiceClientImpl)
VmExportTaskSplitter splitter = new VmExportTaskSplitter()
splitter.assetServiceClient = client
Map map1 = [assetIds:["1","2","3","4","5"],scrollId:null]
client.getAssetIdBatch(_ as String,_ as String, null, _ as Map) >> map1
when:
splitter.splitAndSend(export)
then:
1 * client.getAssetIdBatch(_ as String, _ as String, _ as String, _ as Map)
}
}
Here's the annoying part: both lines on either side of the assetServiceClient.getAssetIdBatch
call are printed. But Spock is claiming there are no invocations whatsoever...
Using logging directory: './logs'
Using log file prefix: ''
test
batch: [assetIds:[1, 2, 3, 4, 5], scrollId:null]
Too few invocations for:
1 * client.getAssetIdBatch(_ as String, _ as String, _ as String, _ as Map) (0 invocations)
Unmatched invocations (ordered by similarity):
None
Upvotes: 1
Views: 1173
Reputation: 5031
Change this line:
1 * client.getAssetIdBatch(_ as String, _ as String, _ as String, _ as Map) (0 invocations)
... on:
1 * client.getAssetIdBatch(_ as String, _ as String, _, _ as Map)
In VmExportTaskSplitter
you pass empty Map
into getAssetIdBatch
method so batch.scrollId
will be null
and it will not match the _ as String
.
Your specification can be also simplified, but it depends on what do you need to test. Guessing from the then
part you test only if the getAssetIdBatch
method was called then it is enough to write it like this:
def "tags should be parsed correctly"() {
setup:
Export export = new Export(containerUuid: "000", userUuid: "000", chunkSize: 10)
AssetServiceClient client = Mock(AssetServiceClient)
VmExportTaskSplitter splitter = new VmExportTaskSplitter()
splitter.assetServiceClient = client
when:
splitter.splitAndSend(export)
then:
1 * client.getAssetIdBatch('000', '000', null, [:])
}
Upvotes: 1