从Groovy-Spock代码

时间:2016-10-25 03:41:34

标签: groovy where-clause spock

我为我的应用程序编写了代码。

def "Test for file type #FileFormat"() {
      given:
      HttpURLConnection connection = getHandlerURL('endpoint')
      connection.setDoOutput(true)
      connection.setRequestMethod("POST")
      connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "RdfFormat."+RDFFileFormat+".toMIME()")
      rdfStatement = ModelFactory.createDefaultModel().read(new ByteArrayInputStream(readRDFfromfile(Filename).bytes), null, "RdfFormat."+RDFFileFormat.toString()).listStatements().nextStatement()

      when:
      connection.getOutputStream().write(readRDFfromfile(Filename).bytes)

      then:
      connection.getResponseCode() == HTTP_CREATED


      where:
      FileFormat      | Filename           | RDFFileFormat
      'N-TRIPLES'     | 'n-triples.nt'     | "NTRIPLES"
}

当我运行我的代码时,我在Given子句的最后一行收到错误:SampleTest.Test for file type #FileFormat:37 » Riot
如果我使用RdfFormat.NTRIPLES.toString()而不是使用从Where子句传递的参数 RDFFileFormat ,则测试正在通过。
尝试分配def format1 = "RdfFormat."+RDFFileFormat+".toString()"并使用 format1 ,但同样的错误。

有什么方法可以让它发挥作用吗?

1 个答案:

答案 0 :(得分:1)

我想你可能想要:

connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, RdfFormat."$RDFFileFormat".toMIME())