Passing actual parameters in a spock unit test specification
Asked Answered
A

3

5
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.

However, since I am new to spock, I am not sure how I can pass in the actual parameters to get a correct output?

This is the function signature I want to test, which takes in an inputStream, char[], and a 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. 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,

Ardelia answered 4/7, 2014 at 8:6 Comment(1)
The title is currently "Passing actual parameters in a spock unit test specification". This question doesn't really have anything to do with parameter passing. You should give the question a title which better describes what is being asked.Interlocutory
H
4

I think the where part accepts only static or shared fields. 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. So if you need to enclose the RESULT with double quotes(")

Highboy answered 7/7, 2014 at 13:23 Comment(0)
C
4

No such property issue is due to the where: block.

the where block first initializes the test fields. in your case inputStream, keystorePwd and url are undeclared, that is why you get the No such property error.

Either Initialize the fields in the where block, remove the where block, declare the fields in the class.

Cyanine answered 7/7, 2014 at 8:29 Comment(0)
H
4

I think the where part accepts only static or shared fields. 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. So if you need to enclose the RESULT with double quotes(")

Highboy answered 7/7, 2014 at 13:23 Comment(0)
T
1

You missed new

def inputStream = new FileInputStream("../resources/keystore.bks")
Try answered 4/7, 2014 at 9:16 Comment(3)
I added the new. But I don't think that is the problem: As I now get this error: No such property: inputStreamArdelia
There's also no need to use where block here. You can inline all the variables used in this line snapzClient.makeRequest(A, B, C) == RESULT.Wheels
The line I am getting this error is: def inputStream = new FileInputStream("../resources/keystore.bks") The FileInputStream should return a InputStream to the bks file, and that inputStream gets passed to the function I am testing. However, am I allowed to use java syntax in groovy spock testing?Ardelia

© 2022 - 2024 — McMap. All rights reserved.