1

I have 2 string arrays in a class. I have a TestNG class in which I need to compare the values of those arrays in a Test method. The idea is, I should loop my Test method for n number of times where n = {size of one of the arrays}

@Test(invocationCount = {fixedCount}) does not work for me, since size of the array varies.

Please let me know if I have to provide some more information.

AndyN
  • 2,075
  • 16
  • 25
Lawrence
  • 29
  • 1
  • 6

1 Answers1

0

How about to check first, if the size of the two arrays are equal? If they are not equal, then the two arrays won't be equal either.

Edit:

This is a thread about equality checking using Java. If I remember well, Arrays.equals(array1, array2) is the proper way for contained element checking, but I'm not a Java guy.
If you'd like to check the elements by hand, use iteration on the arrays (you can, since their size equals now).

bool validator(int[] array1, int[] array2)
{
    if (array1 == null || array2 == null)
    {
        return false;
    }

    if (array1.length != array2.length)
    {
        return false;
    }

    for (int i = 0; i < array1.length; i++)
    {
        if (array1[i] != array2[i])
        {
            return false;
        }
    }

    return true;
}
Community
  • 1
  • 1
aniski
  • 1,263
  • 1
  • 16
  • 31
  • Thanks for your answer KAI. How to proceed when the size of the two arrays are equal? My requirement is, I need to compare the arrays like: `array1[0] == array2[0] \\ first assertion in Test method` and then `array1[1] == array2[1] \\ second assertion in the same Test method` – Lawrence May 27 '16 at 11:09
  • OK. Sorry for not being precise in my question. This is what I have: `HashMap map1 = new HashMap();` `HashMap map2 = new HashMap();` I need to iterate through the HashMap like: `for(int i=0; I – Lawrence May 27 '16 at 11:44