Praveen Praveen - 1 month ago 9
Groovy Question

Getting error when passing parameters from Where block in Groovy-Spock code

I have written a code for my application.

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"
}


When I run my code I am getting error:
SampleTest.Test for file type #FileFormat:37 ยป Riot
in last line of Given clause.

The test is passing if I use
RdfFormat.NTRIPLES.toString()
instead of using the parameter RDFFileFormat passed from Where clause.

Tried assigning
def format1 = "RdfFormat."+RDFFileFormat+".toString()"
and using format1, but got same error.

Is there any way I can make it work?

Answer

I think you probably want:

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