2

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

  1. I must control always the counter
  2. If I add a new variable for the where block, such as mediaType, then I must create a new method such as methodNames()
  3. 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

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

4

2 回答 2

0

你可以试试这个:

where: "Internal"

uriRequestReport << [ControllerSupportTest.createUrl1(), ControllerSupportTest.createUrl2()]
methodNameReport = [ControllerSupportTest.METHODNAME_FINDONE_BYID]
于 2019-02-09T01:14:12.803 回答
0

只需编写将返回一组条目的方法(),其中每个条目都有一次测试运行所需的所有数据。url 的 ArrayList、methodnames 的 ArrayList 和其他数据的 ArrayList。然后将所有列表放在一个大的 ArrayList 或映射中并返回此对象。

{
 "urls":[list of urls]
 "methods":[list of methodNames]
 "otherdata":[listOfOtherData]
}

直接返回地图,或制作此类地图的数组列表。

于 2016-10-28T14:17:14.313 回答