简体   繁体   English

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

[英]Passing actual parameters in a spock unit test specification

org.spockframework:spock-core:0.7-groovy-2.0
Gradle 1.12
Groovy 1.8.6
java

Hello, 你好,

I am trying to use spock with my java application to run unit tests and building with gradle . 我正在尝试使用spock和我的java应用程序来运行单元测试并使用gradle

However, since I am new to spock, I am not sure how I can pass in the actual parameters to get a correct output? 但是,由于我是spock的新手,我不确定如何传递实际参数以获得正确的输出?

This is the function signature I want to test, which takes in an inputStream, char[], and a String: 这是我想要测试的函数签名,它接受inputStream,char []和String:

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

So in my test specification, I want to pass the keystore file as an inputStream where the actual keystore is located here ../resources/keystore.bks, and the actual password for the keystore and the url to where the web service is. 因此,在我的测试规范中,我想将keystore文件作为inputStream传递,其中实际的密钥库位于此处../resources/keystore.bks,以及密钥库的实际密码和Web服务所在的URL。 However, I get this error when trying to run the unit test: 但是,我在尝试运行单元测试时遇到此错误:

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

My specification test is below, but I think I am going about this the wrong way. 我的规格测试如下,但我认为我的方法是错误的。

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
    }
}

Many thanks for any suggestions, 非常感谢任何建议,

No such property issue is due to the where: block. No such property问题是由于where: block。

the where block first initializes the test fields. where块首先初始化测试字段。 in your case inputStream , keystorePwd and url are undeclared, that is why you get the No such property error. 在你的情况下, inputStreamkeystorePwdurl是未声明的,这就是你得到No such property错误的原因。

Either Initialize the fields in the where block, remove the where block, declare the fields in the class. 初始化where块中的字段,删除where块,声明类中的字段。

I think the where part accepts only static or shared fields. 我认为where部分只接受静态或共享字段。 Or else the value need to be a hard coded literal. 否则值必须是硬编码的文字。 So when I modified the class to make the parameters shared it worked for me. 因此,当我修改类以使参数共享时,它对我有效。 Please try this 请试试这个

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"
    }
}

Please note that the return type of makeRequest() method is string. 请注意, makeRequest()方法的返回类型是string。 So if you need to enclose the RESULT with double quotes(") 所以如果你需要用双引号括起RESULT(“)

你错过了new

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM