Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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,

like image 951
ant2009 Avatar asked Feb 12 '23 14:02

ant2009


1 Answers

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(")

like image 55
Syam S Avatar answered Feb 15 '23 09:02

Syam S