Groovy默认方法参数

时间:2013-03-07 20:16:11

标签: groovy overloading

如何调用以下方法def只传递 interactionId,transactionId,awResponseFile,testMethodName和expectedNumberOfConsumers

方法def

void verifyPersistence(String interactionId, String transactionId, 
                       File rawResponseFile, String testMethodName, 
                       int expectedNumberOfDatasourceProductResponseRaws=1, 
                       String dsProductName=datasourceSpec['creditreport.name'], 
                       int expectedNumberOfDatasourceProductResponses=1, 
                       int expectedNumberOfConsumers=1, 
                       int expectedNumberOfAddresses=1 )

方法调用

verifyPersistence interactionId.persistedVersion(), transactionId, 
                  results, testName.methodName, expectedNumberOfConsumers:8

异常

groovy.lang.MissingMethodException: No signature of method: verifyPersistence() is applicable for argument types: (java.util.LinkedHashMap, java.lang.String, java.lang.String, java.io.File, java.lang.String) values: [[expectedNumberOfConsumers:8], 3130001030065, 10003000000890059, C:\results, multiple consumers contracts]
Possible solutions: 

verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String), 
verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String, int), 
verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String, int, java.lang.String), 
verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String, int, java.lang.String, int), verifyPersistence(java.lang.String, java.lang.String, java.io.File, java.lang.String, int, java.lang.String, int, int)
    at HitmultipleconsumersPersistSpec.groovy:151)

1 个答案:

答案 0 :(得分:4)

当您包含地图条目(key:value)时,它们会自动收集,放入散列图并作为第一个参数传递。 Groovy,现在,没有命名参数。对于那个特定情况,我认为你最好使用该方法的地图:

def verifyPersistence(Map map) {
  String interactionId = map.interactionId
  String transactionId = map.transactionId
  File rawResponseFile = map.rawResponseFile
  String testMethodName = map.testMethodName
  int expectedNumberOfDatasourceProductResponseRaws = map.expectedNumberOfDatasourceProductResponseRaws ?: 1
  String dsProductName = map.dsProductName ?: datasourceSpec['creditreport.name']
  int expectedNumberOfDatasourceProductResponses = map.expectedNumberOfDatasourceProductResponses ?: 1
  int expectedNumberOfConsumers = map.expectedNumberOfConsumers ?: 1
  int expectedNumberOfAddresses = map.expectedNumberOfAddresses ?: 1
}


verifyPersistence(
  interactionId : interactionId.persistedVersion(), 
  transactionId : transactionId, 
  rawResponseFile : results, 
  expectedNumberOfDatasourceProductResponseRaws : 14, 
  expectedNumberOfConsumers:8
)

您显然不需要将每个参数都分配给变量,您只需使用map.field语法即可。如果您希望对其进行类型检查,那么我认为您最好使用对象来封装参数。