在 junit 中断言 arrayList 内容时出现断言错误
assertion error while asserting arrayList contents in junit
@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()));
}
导致以下错误:
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
和 equals
被覆盖如下:
@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;
}
已经使用 assertTrue
和 assertArrayEquals
尝试了此处提供的解决方案 ArrayList equality JUnit testing,但没有成功
我错过了什么吗?
更新:
试过如下:
@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()} );
}
导致错误:
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>
和
@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()} );
}
导致
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>
通常情况下,
答案是注意你写的代码.
查看您在问题中包含的 equals 方法。
回答这个问题:当为真时返回什么值 obj != null && obj instanceof Achievement
.
一旦你回答了这个问题,
解决问题。
@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()));
}
导致以下错误:
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
和 equals
被覆盖如下:
@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;
}
已经使用 assertTrue
和 assertArrayEquals
尝试了此处提供的解决方案 ArrayList equality JUnit testing,但没有成功
我错过了什么吗?
更新:
试过如下:
@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()} );
}
导致错误:
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>
和
@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()} );
}
导致
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>
通常情况下, 答案是注意你写的代码.
查看您在问题中包含的 equals 方法。
回答这个问题:当为真时返回什么值 obj != null && obj instanceof Achievement
.
一旦你回答了这个问题, 解决问题。