Trying to convert Junit parameterized test to spock need some help

568 views
Skip to first unread message

Barry

unread,
Aug 2, 2011, 12:16:36 PM8/2/11
to Spock Framework - User
Hello,

I have found examples of this on the site but I could not successfully
translate that to what I wanted to do. My question is how to read
from a file in the where clause . In my JUnit test I read in a CSV and
do some processing for the @Test method. Here is what I tried

java:
@Parameters
public static Collection<Object []> generateData(){
Object[][] dataArray = new Object[number_of_records][2];
try {
File f = new File("/Users/bkern/Desktop/table_export.csv");
BufferedReader in = new BufferedReader(new FileReader(f));
String line;
int count =0;
while((line = in.readLine()) != null) {
String parts[] = line.split(",");
dataArray[count][0] = parts[0];
dataArray[count][1] = parts[1];
count++;
}
return Arrays.asList(dataArray);
} catch (Exception e){
System.out.println("Caught exception: " + e );
}
return Arrays.asList(new Object[0][0]);

groovy:
List lines = new File("/Users/bkern/table_export5.csv").readLines()
int count =0;
lines.each{ line ->
String parts[] = line.split(",");
dataArray[count][0] = parts[0];
dataArray[count][1] = parts[1];
count++;

}

return Arrays.asList(dataArray);

then I get this message when I try to run my test:
where-blocks may only contain parameterizations (e.g. 'salary <<
[1000, 5000, 9000]; salaryk = salary / 1000') @ line 42, column 8.
where:
^

so I know I am doing something wrong but not really sure where to go.
At a high level I am reading in a CSV of encrypted HTTP requests,
responses and I use the parameterized test to load the encrypted
reuquests and responses into an array then the test decrypts each
request and response.


Thanks,
Barry

Peter Niederwieser

unread,
Aug 2, 2011, 12:21:41 PM8/2/11
to spockfr...@googlegroups.com
Please include the relevant code of your Spock test (method).

Cheers,
Peter

> --
> You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
> To post to this group, send email to spockfr...@googlegroups.com.
> To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.
>

Barry Kern

unread,
Aug 2, 2011, 12:31:24 PM8/2/11
to spockfr...@googlegroups.com
Below is the entire class as it is now - it doesn't really test anything significant yet but I wanted to try to start bringing over some more complex tests into spock . 

Something else I was unclear about was converting the constructor from my JUnit test which was 

public AuditDynamicTest(String request,String response){

this.request = request;

this.response = response;

}


so I  defined these instance variables before any feature methods and made a guess that in setup perhaps I could initialized these but given my error message I wonder if these instance variables should be on the left side of << in the where clause.

groovy class :

class DecryptionParameterized extends Specification {

def request;

def response;

@Shared factory

static final number_of_records = 65


def setupSpec(){

factory = EncryptionFactory.getInstance()

Object[][] dataArray  = new Object[number_of_records][2];

}

 

def "trying to bring this into spock"() {

setup:

this.request = request

this.response = response

 

when: 

String token1 = factory.decrypt(this.request).toString();

System.out.println(new String(StringUtils.substringBeforeLast(token1, ">") + ">"));



String token1_response = factory.decrypt(this.response).toString();

System.out.println(new String(StringUtils.substringBeforeLast(token1_response, ">") + ">"));

then:

token1 != null

token1_response != null

token1.length() > 0

token1_response.length() > 0

where:

Peter Niederwieser

unread,
Aug 2, 2011, 12:56:53 PM8/2/11
to spockfr...@googlegroups.com
The most important thing to realize is that in Spock, parameterization is per test method, not per test class (as in JUnit).

Obviously I can't run your spec, but I've edited it to hopefully get you closer to a working solution (and to make it a bit more Groovy).

Cheers,
Peter

class DecryptionParameterized extends Specification {
    def factory = EncryptionFactory.getInstance()
     
    def "trying to bring this into spock"() { // optionally you can define "request" and "response" as parameters of this method (may give better IDE support)
        when: 
            def token1 = factory.decrypt(request).toString()
            println(StringUtils.substringBeforeLast(token1, ">") + ">")

            def token1_response = factory.decrypt(response).toString()
            println(StringUtils.substringBeforeLast(token1_response, ">") + ">")
        
        then:
            token1 != null
            token1_response != null
            token1.size() > 0
            token1_response.size() > 0
        where:  
            // assuming a line has just two values
            [request, response] << new File("/Users/bkern/table_export5.csv").readLines()*.split(",")

            // otherwise
            line << new File("/Users/bkern/table_export5.csv").readLines()*.split(",")
            request = line[0]
            response = line[1]
    }
}

On 02.08.2011, at 18:31, Barry Kern wrote:

groovy class :

class DecryptionParameterized extends Specification {

def factory = EncryptionFactory.getInstance()

 

def "trying to bring this into spock"() {

when: 

String token1 = factory.decrypt(request).toString()

println StringUtils.substringBeforeLast(token1, ">") + ">"



String token1_response = factory.decrypt(this.response).toString()

println StringUtils.substringBeforeLast(token1_response, ">") + ">"


then:

token1 != null

token1_response != null

token1.length() > 0

token1_response.length() > 0

where:

                                                        // assuming a line has just two values

[request, response] << new File("/Users/bkern/table_export5.csv").readLines()*.split(",")


                                                           // otherwise
                                                        line << new File("/Users/bkern/table_export5.csv").readLines()*.split(",")
                                                           request = line[0]
                                                           response = line[1]

Barry Kern

unread,
Aug 2, 2011, 2:51:24 PM8/2/11
to spockfr...@googlegroups.com
Thanks that worked
Reply all
Reply to author
Forward
0 new messages