2017-08-16 59 views
2

用於參數化單元測試在groovy我已經準備的數據:Groovy數組。 。在它們之間[[],[]]差指定者()和[[],[]] *指定者()

@Parameterized.Parameters 
static List data() { 
    [ 
      ['20101113', 'DATE', 
      '20101114', 'DATE', 
      'some statement', 
      '20101101T000000/20110101T000000', 
      null, 
      ['some', 'expected', 'data', 'results']] 
    // ...and other 
    ]*.toArray() 
} 

它能夠正常工作。我檢查了這個代碼,試圖用].toArray()簡化替換]*.toArray()

java.lang.Exception: FirstParametrizedTest.data() must return a Collection of arrays. 

at org.junit.runners.Parameterized$TestClassRunnerForParameters.computeParams(Parameterized.java:94) 
at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:86) 
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:236) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:24) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:157) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 

我恢復了我的變化回來了,它的工作原理,但我不明白爲什麼指令].toArray()是意外? ].toArray()如何工作以及].toArray()]*.toArray()之間的區別是什麼?

+1

'* .'是一個傳播點運算符。希望這個例子會有所幫助 - http://mrhaki.blogspot.in/2009/08/groovy-goodness-spread-dot-operator.html – Rao

回答

2

doc*.被稱爲展開運算符並返回一個列表。

傳播運算符(*.)用於對聚合對象的所有項目調用動作。這等同於調用每個項目的行動和結果收集到一個列表:

沒有*toArray只是返回一個對象數組,當然陣列不能投在運行時列出。


隨着*當前的代碼做這樣的事情:

static List data() { 
     def result = [] 
     List input = [ 
       ['20101113', 'DATE', 
       '20101114', 'DATE', 
       'some statement', 
       '20101101T000000/20110101T000000', 
       null, 
       ['some', 'expected', 'data', 'results']] 
       // ...and other 
     ] 

     for (def item : input) { 
      result.add(item != null ? ((List) item).toArray() : null) 
     } 

     result 
    } 
相關問題