Reputation: 2002
@Test
public void testAchievementsSize(){
Player player = getPlayer();
List<Achievement> playerAchievements = achievementManager.checkForPlayerAchievements(preparePlayerStatistics(player));
assertEquals(3,playerAchievements.size());
assertThat(playerAchievements, containsInAnyOrder(new BruiserAward(),new SharpShooterAward(),new FastKillerAward()));
}
causing the below error:
java.lang.AssertionError:
Expected: iterable over [<Bruiser>, <Sharp shooter>, <Fast Killer>] in any order
but: Not matched: <Bruiser>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
Hashcode
and equals
is overridden like below:
@Override
public int hashCode() {
return this.getName().hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof Achievement){
Achievement achievement = (Achievement) obj;
achievement.getName().equals(this.getName());
}
return false;
}
Have tried solutions ArrayList equality JUnit testing present here using assertTrue
and assertArrayEquals
but no luck
Am I missing anything?
Update:
Tried below:
@Test
public void testAchievementsSize(){
Player player = getPlayer();
List<Achievement> playerAchievements = achievementManager.checkForPlayerAchievements(preparePlayerStatistics(player));
assertEquals(3,playerAchievements.size());
assertArrayEquals(playerAchievements.toArray(new Achievement[playerAchievements.size()]),
new Achievement[]{new BruiserAward(),new SharpShooterAward(),new FastKillerAward()} );
}
causing error:
arrays first differed at element [0]; expected: com.techdisqus.achievement.BruiserAward<Bruiser> but was: com.techdisqus.achievement.BruiserAward<Bruiser>
Expected :com.techdisqus.achievement.BruiserAward<Bruiser>
Actual :com.techdisqus.achievement.BruiserAward<Bruiser>
and
@Test
public void testAchievementsSize(){
Player player = getPlayer();
List<Achievement> playerAchievements = achievementManager.checkForPlayerAchievements(preparePlayerStatistics(player));
assertEquals(3,playerAchievements.size());
assertArrayEquals(playerAchievements.toArray(),
new Achievement[]{new BruiserAward(),new SharpShooterAward(),new FastKillerAward()} );
}
causing
arrays first differed at element [0]; expected: com.techdisqus.achievement.BruiserAward<Bruiser> but was: com.techdisqus.achievement.BruiserAward<Bruiser>
Expected :com.techdisqus.achievement.BruiserAward<Bruiser>
Actual :com.techdisqus.achievement.BruiserAward<Bruiser>
Upvotes: 1
Views: 714
Reputation: 38300
As is often the case, the answer is Pay attention to the code you write.
Look at the equals method you included in your question.
Answer this question: What value is returned when this is true obj != null && obj instanceof Achievement
.
Once you answer that question, fix the problem.
Upvotes: 2