1
 @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>
2
  • Can you show us checkForPlayerAchievements method code? Commented Dec 21, 2018 at 17:45
  • The deleted answer seems correct. Check the equals method of the BruserAward class. Commented Dec 21, 2018 at 20:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Silly me, this miss cost me few hours.. but thank you for showing it to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.