I am working with:
- Spring MVC
- Spring MVC Test
- Spock Framework
About Spock working with @Unroll
I have the following situation about the where
block
I can have the following:
where: "Internal"
uriRequestReport << [ControllerSupportTest.createUrl1(), ControllerSupportTest.createUrl2()]
methodNameReport << [ControllerSupportTest.METHODNAME_FINDONE_BYID, ControllerSupportTest.METHODNAME_FINDONE_BYID ]
Here the part where that is annoying in some way is that the data must be similar about the amount of items or structure, in this case by 2.
I mean, the following fails:
where: "Internal"
uriRequestReport << [ControllerSupportTest.createUrl1(), ControllerSupportTest.createUrl2()]
methodNameReport << [ControllerSupportTest.METHODNAME_FINDONE_BYID]
How you can see now methodNameReport
has one item.
Spock throws an exception.
Since I can test many URLs, it makes verbose be repeating the ControllerSupportTest.METHODNAME_FINDONE_BYID
term many times as many times I use ControllerSupportTest.createUrl#()
where # would be 5 for example
In some way I can handle in the following way:
Note: It is a Java class
public class MyDataSetUtil {
private static final Integer COUNTER = 2;
private MyDataSetUtil(){
}
public static List<String> uris(){
String[] uris = new String[COUNTER];
uris[0] = ControllerSupportTest.createUrl1()
uris[1] = ControllerSupportTest.createUrl2()
}
public static String[] methodNames(){
String[] methodNames = (String[])
createArray(ControllerSupportTest.METHODNAME_FINDONE_BYID);
return methodNames;
}
private static String[] createArray(String value){
String[] objects = new String[COUNTER];
for(int i=0; i<COUNTER; i++){
objects[i] = value;
}
return objects;
}
}
Until here I have two kinds of problems
- I must control always the counter
- If I add a new variable for the
where
block, such asmediaType
, then I must create a new method such asmethodNames()
- Always I must repeat the same data for each item in the array such as
ControllerSupportTest.METHODNAME_FINDONE_BYID
For the two previous points, it is reflected as below:
For example:
public static String[] mediaTypes(){
String[] mediaTypes = (String[]) createArray(MediaType.APPLICATION_XML_VALUE);
return mediaTypes;
}
I need something more practical
I've read the following post from SO too
- Spock: Reading Test Data from CSV File
- Utility API - CSVReader (source code from above)
But for my situation I don't want use CSV yet.
Other approach is through SQL such as:
But again, for my situation I don't want use SQL yet.
For me is more interesting this approach:
In that post he has:
where:
[_, name, gender, expected] << new MultilineProvider(source: userData)
See the MultilineProvider
code from the post.
therefore I want do the following:
[uriRequestReport, methodNameReport, moreVariablesifExists] << [MyDataSetUtil.theMethod()]
Requirement:
I want return through the MyDataSetUtil.theMethod()
(see the line shown above) a collection where each item represent the same than
uriRequestReport << [ControllerSupportTest.createUrl1(), ControllerSupportTest.createUrl2()]
methodNameReport << [ControllerSupportTest.METHODNAME_FINDONE_BYID, ControllerSupportTest.METHODNAME_FINDONE_BYID ]
To let me easily add a new variable in the where
block and update the Java class quickly and avoid repeat the same data many times.
What could be the best approach? Remember the code should be based in Java.
I did a research on Google without a solution.
If you have a better suggestion, it is welcome