简体   繁体   中英

How to assert ArrayList of String ArrayList equal in TestNG

I have a method that groups a given String ArrayList into ArrayList of String ArrayList, with each of the String ArrayList containing strings of the same length. The method is:

public static ArrayList> getGrouping(ArrayList strlist)

 {

    Map<Integer, ArrayList<String>> mapOfList = new HashMap<Integer, ArrayList<String>>();  
    /*
     * Define ArrayList of ArrayList to build the output to return, and, 
     *another ArrayList to act as sub-array to build the output
    */
    ArrayList<ArrayList<String>> outer = new ArrayList<ArrayList<String>>();
    ArrayList<String> inner = new ArrayList<String>();
    String temp;      

    /*
     * populate the map of lists in the format map <length of string , list of strings of this length
     */
    for(int i = 0; i <strlist.size(); i++)
    {
        temp = strlist.get(i);  
        if (temp ==  null)
            continue;
        int len = temp.length();
        len = new Integer(len);
        if(mapOfList.get(len) == null)
            {
                mapOfList.put(len, new ArrayList<String>());
            }
        mapOfList.get(len).add(temp);   
    }

    /*
     * Build sub ArrayList using ArrayList at every entry and add this to the outer ArrayList to be returned
     */
    for(Map.Entry<Integer, ArrayList<String>> entry : mapOfList.entrySet())
        {           
            inner = entry.getValue();           
            outer.add(inner);           
        }
   return outer;
    }

I am new to TestNG and trying to write test methods for the above. I am having trouble building the expected result (which is ArrayList of String ArrayList). What's the best way to do it? The way I do it now throws the error:

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Also they way I build the input is probably not the best way. Could this be done better? The TestNG @Test method I have now which fails is:

public void testWithNullValues() 
   {
       ArrayList<ArrayList<String>> oup = new ArrayList<ArrayList<String>>();
       ArrayList<String> inp = new ArrayList<String>();
       inp.add("abc");
       inp.add("ba");
       inp.add("bdc");
       inp.add(null);
       inp.add("bfed");
       inp.add(null);
       inp.add("bdsf");
       inp.add("a");
       inp.add("b");
       inp.add("bdcjkhwd");
       oup = GroupListOfString.getGrouping(inp);

      ArrayList<ArrayList<String>> expectedResult = new ArrayList<ArrayList<String>>();
      expectedResult.add((ArrayList<String>) Arrays.asList("a", "b"));
      expectedResult.add((ArrayList<String>) Arrays.asList("ba"));
      expectedResult.add((ArrayList<String>) Arrays.asList("abc", "bdc"));
      expectedResult.add((ArrayList<String>) Arrays.asList("bfed","bdsf"));
      expectedResult.add((ArrayList<String>) Arrays.asList("bdcjkhwd"));

      Assert.assertEquals(expectedResult, oup);

      inp.clear();
    }

Thanks in Advance!

Arrays.asList returns a java.util.Arrays.ArrayList , which is not the same as a java.util.ArrayList the rest of your class uses. You could, however, just construct one:

ArrayList<ArrayList<String>> expectedResult = new ArrayList<>();
expectedResult.add(new ArrayList<>(Arrays.asList("a", "b")));
expectedResult.add(new ArrayList<>(Arrays.asList("ba")));
expectedResult.add(new ArrayList<>(Arrays.asList("abc", "bdc")));
expectedResult.add(new ArrayList<>(Arrays.asList("bfed","bdsf")));
expectedResult.add(new ArrayList<>(Arrays.asList("bdcjkhwd")));

Assert.assertEquals(expectedResult, oup);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM