微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 在spock单元测试规范中传递实际参数

org.spockframework:spock-core:0.7-@R_928_404[email protected]
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) {
    ...
}

因此,在我的测试规范中,我想将keystore文件作为inputStream传递,其中实际的密钥库位于此处../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
    }
}

非常感谢任何建议,

解决方法:

我认为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()方法的返回类型是string.所以如果你需要用双引号括起RESULT(“)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐