【问题标题】:Error testing thrown exceptions in Spock unit tests错误测试 Spock 单元测试中抛出的异常
【发布时间】:2013-01-21 20:54:38
【问题描述】:

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

@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 在 org.codehaus.groovy.ast.expr.DeclarationExpression.getVariableExpression(DeclarationExpression.java:103) 在 org.spockframework.compiler.SpecRewriter.moveVariableDeclarations(SpecRewriter.java:538)

【问题讨论】:

  • 尝试运行 grails clean 并重新运行测试
  • 我试过了,但似乎没有帮助
  • 您是否缺少任何导入?
  • 您使用的是哪个 Grails 版本,以及哪个 Spock 版本?

标签: unit-testing grails spock


【解决方案1】:

尝试不利用 Groovy 的多任务功能 (def (privateKey,publicCert) = ...)。如果这能解决问题(我认为会),请在http://issues.spockframework.org 提出问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    相关资源
    最近更新 更多