Testowanie dwóch list obiektów

0

Witam, jak na razie się uczę i utknąłem na jednym problemie
Posiadam klasę Ship, która po wywołaniu getPoints zwraca mi listę obiektów Point
Chciałbym ją przetestować, czy prawidłowe Pointy są zwaracane
W klasie testowej mam

List<Point> points = Arrays.asList(new PointImpl(3, 6), new PointImpl(4, 6), new PointImpl(5, 6));

Próbując ją porównać przez ten sposób

assertThat(ship.getPoints(), containsInAnyOrder(points));

wyrzuca mi

java.lang.AssertionError: 
Expected: iterable over [<[battleship.PointImpl@128d1e9, battleship.PointImpl@98a3ee, battleship.PointImpl@8c58a6]>] in any order
     but: Not matched: <battleship.PointImpl@e261f0>

Pozostaje mi tylko testowanie przez pętle, w której wszystkie testy przechodzą. Czy jest to dobre rozwiązanie?

for(Point p : ship.getPoints()) {
			assertEquals(p.getRow(), points.get(i).getRow());
			assertEquals(p.getColumn(), points.get(i).getColumn());
			i++;
		}
0

A masz w klasie PointImpl equals i hashcode? Bo rozumiesz ze Java to nie jasnowidz i nie wie jak porównać dwa obiekty dopóki tego jakoś nie zdefiniujesz? Domyślne porównanie sprawdza czy to te same obiekty, co u ciebie oczywiście nie będzie prawdą, bo tworzysz dwie osobne listy obiektów. Jeśli dwa PointImpl są sobie równe kiedy row i column są takie same to napisz equals i hashcode które biorą pod uwage te 2 pola.

0

Tak mam

Override
	public boolean equals(Point other) {
		return (row == other.getRow() && column == other.getColumn()) ? true : false;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + column;
		result = prime * result + row;
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		return result;
	}

Teraz dodałem

@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PointImpl other = (PointImpl) obj;
		if (column != other.column)
			return false;
		if (row != other.row)
			return false;
		if (status != other.status)
			return false;
		return true;
	}

i nadal jest to samo
Klasa PointImpl implementuje interface Point

0

W testach klasy Point metoda equals oraz isTheSame przechodziła bez problemu

@Override
	public boolean isTheSame(Point other) {
		return (row == other.getRow() && column == other.getColumn()) ? true : false;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + column;
		result = prime * result + row;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PointImpl other = (PointImpl) obj;
		if (column != other.column)
			return false;
		if (row != other.row)
			return false;
		return true;
	}

Metoda do testowania

@Test
	void IsTheSameReturnTrueWhenThisSamePoint() {
		Point p = new PointImpl(5, 7);
		assertTrue(point.equals(p));
	}
@Test
	void equalsReturnTrueWhenThisSamePoint() {
		Point p = new PointImpl(5, 7);
		assertTrue(point.equals(p));
	}
1

containsInAnyOrder(points.toArray())

0

Brawo. Dziękuję.
Wszędzie gdzie czytałem widziałem, że są porównywane listy obiektów, a tutaj trzeba wrzucić tablice.
Dzięki i pozdrawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1