【问题标题】:Passing actual parameters in a spock unit test specification在 spock 单元测试规范中传递实际参数
【发布时间】:2014-08-25 12:17:11
【问题描述】:
org.spockframework:spock-core:0.7-groovy-2.0
Gradle 1.12
Groovy 1.8.6
java

你好,

我正在尝试将 spock 与我的 java 应用程序一起使用来运行单元测试并使用 gradle 进行构建。

但是,由于我是 spock 新手,我不确定如何传入实际参数以获得正确的输出?

这是我要测试的函数签名,它接受一个 inputStream、char[] 和一个 String:

public String makeRequest(InputStream keystoreFilename, char[] keystorePassword, String cnn_url) {
    ...
}

所以在我的测试规范中,我想将密钥库文件作为输入流传递,其中实际密钥库位于此处 ../resources/keystore.bks,以及密钥库的实际密码和 Web 服务所在的 URL是。但是,在尝试运行单元测试时出现此错误:

groovy.lang.MissingMethodException: No signature of method: com.sunsystem.HttpSnapClient.SnapClientTest.FileInputStream()

我的规范测试如下,但我认为我的做法是错误的。

import spock.lang.Specification;
import java.io.InputStream;
import java.io.FileInputStream;

class SnapClientTest extends Specification {
    def 'Connect to https web service'() {
        setup:
        def snapzClient = new SnapzClient();

        def inputStream = FileInputStream("../resources/keystore.bks")
        def keystorePwd = "password".toCharArray()
        def url = "https://example_webservice.com"

    expect: 'success when all correct parameters are used'
        snapzClient.makeRequest(A, B, C) == RESULT

        where:
        A           | B           | C   | RESULT
        inputStream | keystorePwd | url | 0
    }
}

非常感谢您的任何建议,

【问题讨论】:

  • 标题目前是“在 spock 单元测试规范中传递实际参数”。这个问题实际上与参数传递没有任何关系。您应该给问题起一个能更好地描述所问内容的标题。

标签: java unit-testing groovy spock


【解决方案1】:

我认为where 部分只接受静态或共享字段。否则该值需要是硬编码文字。因此,当我修改类以使参数共享时,它对我有用。请试试这个

import spock.lang.Shared
import spock.lang.Specification

class SnapClientTest extends Specification {

    @Shared def inputStream = new FileInputStream("../resources/keystore.bks")
    @Shared def keystorePwd = "password".toCharArray()
    @Shared def url = "https://example_webservice.com"

    def "Connect to https web service"() {
        setup:
        def snapzClient = new SnapzClient();

        expect: 
        snapzClient.makeRequest(A, B, C) == RESULT

        where:
        A           | B           | C   | RESULT
        inputStream | keystorePwd | url | "0"
    }
}

请注意makeRequest() 方法的返回类型是字符串。因此,如果您需要将 RESULT 括在双引号中 (")

【讨论】:

    【解决方案2】:

    No such property 问题是由 where: 块引起的。

    where 块首先初始化测试字段。 在您的情况下,inputStreamkeystorePwdurl 未声明,这就是您收到 No such property 错误的原因。

    要么初始化where块中的字段,删除where块,声明类中的字段。

    【讨论】:

      【解决方案3】:

      你错过了new

      def inputStream = new FileInputStream("../resources/keystore.bks")
      

      【讨论】:

      • 我添加了新的。但我不认为这是问题所在:我现在得到这个错误:没有这样的属性:inputStream
      • 这里也不需要使用where 块。您可以内联此行中使用的所有变量snapzClient.makeRequest(A, B, C) == RESULT
      • 我得到这个错误的行是: def inputStream = new FileInputStream("../resources/keystore.bks") FileInputStream 应该返回一个 InputStream 到 bks 文件,并且那个 inputStream 被传递到我正在测试的功能。但是,我可以在 groovy spock 测试中使用 java 语法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多