samedi 5 novembre 2016

How to test different values for a method parameter

I'm testing a method that takes a class as a parameter. How can I use spock to set different values for each class property in my test method. This is what I'm trying to do:

def "test stampMetaData" () {
    given:
        myHelper.wordParser = Mock(WordParser) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream()
            baos.write("test for worddoc".getBytes(Charset.forName("UTF-8")))
            stampMetadata(_,_) >> baos
        }
        myHelper.excel3Parser = Mock(ExcelParser) {
            ByteArrayOutputStream baos = new rByteArrayOutputStream()
            baos.write("test for excel ".getBytes(Charset.forName("UTF-8")))
            stampMetadata(_,_) >> baos
        }
        request.fileType = "DOCX"
        request.origFileName = "docxtest.docx"


    expect:
        myHelper.stampMetaData(request).getText() == returnVal

    where:
        request                                                             |    returnVal
        request.fileType = "DOCX, request.fileName = "docxtest.docx"    | "test for worddoc"
        request.fileType = "XLSX, request.fileName = "exceltest.docx"   | "test for excel"

}

I would like to set the fileType and fileName properties of the request class and test it for corresponding returnVal. Is this possible to do in Spock?

Aucun commentaire:

Enregistrer un commentaire