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 传递,其中实际 keystore 位于此处 ../resources/keystore.bks,以及 keystore 的实际密码和 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()
方法的返回类型是字符串。因此,如果您需要用双引号 (") 将 RESULT 括起来
关于java - 在 spock 单元测试规范中传递实际参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24569534/