[英]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
你好,
我正在尝试使用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
}
}
非常感谢任何建议,
No such property
问题是由于where:
block。
where
块首先初始化测试字段。 在你的情况下, inputStream , keystorePwd和url是未声明的,这就是你得到No such property
错误的原因。
初始化where
块中的字段,删除where
块,声明类中的字段。
我认为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(“)
你错过了new
def inputStream = new FileInputStream("../resources/keystore.bks")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.