I have the following test:

@Test public void whenRoadGoodThenReturnCellArrWithRoad() for (int i = 0; i != 8 ; i++) { Board.desc[i][i] = new Place(new Cell(i,i),""); } Board.desc[4][4] = new Officer(new Cell(4,4),"white"); Cell[] result = Board.desc[4][4].move(new Cell(7,7)); Cell[] check = new Cell[2]; check[0] = new Cell(5,5); check[1] = new Cell(6,6); assertThat(check[0].getX(), is(result[0].getX())); assertThat(check[0].getY(), is(result[0].getY())); assertThat(check[1].getX(), is(result[1].getX())); assertThat(check[1].getY(), is(result[1].getY())); assertThat(check.length, is(result.length)); } 

That is: there are two result and check arrays, both of which are filled with objects of the Cell type, and these objects store two fields (coordinates), х and у . I need to compare these two arrays by the value of the coordinates stored in each Cell .

What I wrote in principle works, but I want to do it in a shorter, if possible in one line.

    1 answer 1

    Override the hashcode & equals methods of your Cell object. And use the Arrays.equals(array1, array2); construct Arrays.equals(array1, array2);

    • Most likely it will not help. The hashcode of the array is needed to compare another array, and the hashcode of the array is calculated regardless of what lies in it, at the address. - pavel163
    • one
      only better is not assertTrue(Arrays.equal(..)) , but either assertArrayEquals or assertThat( .., arrayContaining(..) ) , with an error in the test there will be a more informative message - zRrr