2015-02-09 112 views
-1

我正在測試我的代碼,使用JUnit4來查看插入排序的對象數組(Word(String a, int b))是否正確排序數組。我遇到的問題是,當我運行JUnit時失敗,給我一個錯誤:「期望{One,1},但是{One,1}。」如果我在運行測試之前打印出兩個值,他們也是一樣的。該代碼是:JUnit4(assertEquals)說對象是不同的,當它們是相同的?

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); 
    } 
} 

的插入排序的代碼:

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 + "}"); 
    } 
} 

任何幫助,將不勝感激,謝謝!

+4

請問您的Word類重寫等於? – 2015-02-09 21:55:36

+0

請在問題中包含相關的代碼,而不是鏈接到它。 – rgettman 2015-02-09 21:56:44

+0

對不起,這只是很多代碼,我不確定它會不會太多。 – 2015-02-09 21:58:02

回答

2

您正在創建兩個不同的對象。僅僅因爲他們的屬性具有相同的價值,他們並不一定是平等的。要達到此目的,您需要覆蓋Word類中的equals()方法。

因此,添加:

@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類,選擇Source - >生成hashCode()和equals()... 選擇在檢查兩個Word對象時應考慮的屬性是否相等。

此外,你應該更好地hashCode()。 相關問題:

順便說一句: 可能是一個副本&粘貼的問題,但是從接口實現的方法沒有被使用@Override(註釋爲您compareTo()是)。由於您覆蓋toSting()-類Object的方法,所以@Override註釋將適用於toString()

@Override的Javadoc:

Indicates that a method declaration is intended to override a method declaration in a supertype.

+0

我明白如何覆蓋equals方法來檢查一個對象是否與另一個對象相同。但是,如何在我的Word類中使用equals來檢查一個對象數組是否等於另一個數組? – 2015-02-09 22:30:28

+0

無需擔心。當JUnit比較數組的相等性時,它在內部調用。順便說一句:你應該使用'assertArrayEquals()',因爲'assertEquals()'被棄用。 – 2015-02-09 22:50:46

+0

對不起,但你可以告訴我和重寫'equals()'的例子。我已經嘗試了許多事情,每次JUnit仍然說失敗。我不確定是否正確覆蓋它,因爲如果我把'@ Override'放在它上面,eclipse告訴我幾乎要擦除它然後運行。 – 2015-02-10 00:20:35

1

JUnit的assertEquals取決於你正確實施Object.equals(Object),你沒有這樣做。在Word中執行equals(Object)以完成此項工作。

相關問題