在Spock单元测试中测试抛出的异常时出错

时间:2013-02-06 15:35:42

标签: unit-testing grails spock

当我尝试使用以下代码测试抛出的异常时:

@TestFor(EncryptionService)
class EncryptionServiceSpec extends Specification {
def "test decryption of unecnrypted file"(){
    setup: 
        def clearTextFile = new File("test/resources/clearText.txt")
        clearTextFile.write("THIS IS CLEAR TEXT")

    when:
        def (privateKey,publicCert) = service.generateKeyPair("123")
        service.decryptFile(new FileInputStream(clearTextFile), privateKey )

    then:
        clearTextFile.delete()
        thrown GeneralSecurityException
}
}

当我运行grails test-app -unit

时,我得到以下编译异常
  

编译规范'com.genospace.services.EncryptionServiceSpec'时出现意外错误。也许您使用了无效的Spock语法?无论如何,请在http://issues.spockframework.org提交错误报告。

     

java.lang.ClassCastException:org.codehaus.groovy.ast.expr.ArgumentListExpression无法强制转换为org.codehaus.groovy.ast.expr.VariableExpression       at org.codehaus.groovy.ast.expr.DeclarationExpression.getVariableExpression(DeclarationExpression.java:103)       在org.spockframework.compiler.SpecRewriter.moveVariableDeclarations(SpecRewriter.java:538)

1 个答案:

答案 0 :(得分:1)

尝试不使用Groovy的多重赋值功能(def (privateKey,publicCert) = ...)。如果这解决了问题(我认为会解决),请在http://issues.spockframework.org提交问题。