我正在使用JUnit4测试我的代码,以查看对象数组(Word(String a, int b)
)的插入排序是否正确排序数组。我遇到的问题是,当我运行JUnit时它会失败,给我一个错误:“预期{One,1}但是{One,1}。”如果我在运行测试之前打印出我正在比较的两个值,它们也是相同的。代码是:
package sort;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class InsertionTest {
private Word word1;
private Word word2;
private Word word3;
private Word word4;
private Word wordExpected1;
private Word wordExpected2;
private Word wordExpected3;
private Word wordExpected4;
@Before
public void setUp() throws Exception {
word1 = new Word("One", 1);
word2 = new Word("Two", 2);
word3 = new Word("Three", 3);
word4 = new Word("Four", 4);
wordExpected1 = new Word("One", 1);
wordExpected2 = new Word("Two", 2);
wordExpected3 = new Word("Three", 3);
wordExpected4 = new Word("Four", 4);
}
@After
public void tearDown() throws Exception {
}
@SuppressWarnings("deprecation")
@Test
public void test() {
Word[] wordList = { word3, word2, word4, word1 };
Word[] expected = { wordExpected1, wordExpected2, wordExpected3, wordExpected4 };
Insertion.sortInsert(wordList);
assertEquals(expected, wordList);
}
}
insertionsort的代码:
package sort;
public class Insertion {
/**
* regular insertion sort
* @param x - the input array containing scores of words that need to be sorted.
*/
public static void sortInsert ( Word[] x) {
int N = x.length;
for (int i = 1; i < N; i++){
int tempScore = x[i].getScore();
String tempWord = x[i].getWord();
int j;
for (j = (i - 1); j >= 0 && tempScore < x[j].getScore(); j--){
x[j + 1].setScore(x[j].getScore());
x[j + 1].setWord(x[j].getWord());
}
x[j + 1].setScore(tempScore);
x[j + 1].setWord(tempWord);
}
}
}
ADT的代码:
package sort;
public class Word implements Comparable<Word>{
private String word;
private int score;
public Word(String w, int s){
this.word = w;
this.score = s;
}
public int getScore(){
return score;
}
public void setScore(int s){
score = s;
}
public String getWord(){
return word;
}
public void setWord(String w){
word = w;
}
@Override
public int compareTo(Word w){
if ((this.score) > (w.score)) { return 1; }
else if ((this.score) < (w.score)) { return -1; }
return 0;
}
public String toString(){
return ("{" + this.word + "," + this.score + "}");
}
}
任何帮助将不胜感激,谢谢!
答案 0 :(得分:2)
您正在创建两个不同的对象。仅仅因为它们的属性具有相同的值,它们不一定相等。要实现此目的,您需要覆盖课程equals()
中的Word
方法。
因此,添加:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Word other = (Word) obj;
if (score != other.score)
return false;
if (word == null) {
if (other.word != null)
return false;
} else if (!word.equals(other.word))
return false;
return true;
}
Eclipse提供了一种自动执行此操作(简单)的简便方法。打开Word
课程,选择来源 - &gt;生成hashCode()和equals()...
选中在检查两个Word
个对象时是否应该考虑的属性。
另外,你应该oderride hashCode()
。
相关问题:
顺便说一下:
可能是复制和粘贴问题,但接口中实现的方法未使用@Override
进行注释(因为您的compareTo()
是)。 @Override
注释适用于toString()
,因为您覆盖了toSting()
- 类Object
的方法。
来自@Override
Javadoc:
表示方法声明旨在覆盖超类型中的方法声明。
答案 1 :(得分:1)
JUnit&#39; assertEquals
取决于您是否正确实施了Object.equals(Object)
,而您并没有这样做。在equals(Object)
中实施Word
以使其发挥作用。
答案 2 :(得分:0)
使用Lombok生成equal和hashcode方法。然后它将起作用。通过使用Lombok批注,您的代码也将变得干净。