2015-02-10 29 views
0

我遇到了java中的assertArrayEquals問題。這是要求我創建一個新的方法,但我使用JUnit,所以我真的不明白我該如何解決這個問題。assertArrayEquals在Junit中不起作用

這是我的JUnit測試案例

@Test 
    public void testSortInsert() throws IOException { 
     Word tester1 [] = input(0,16); 
     Word tester2 [] = input(1,256); 
     Word tester3 [] = input(2,256); 
     Insertion.sortInsert(tester1); 

     Word insert1 [] = input(0,16); 
     StopWatch time = new StopWatch(); 
     time.start(); 
     Insertion.sortInsert(insert1); 
     time.stop(); 
     int insertTime = timer(time); 
     assertArrayEquals(insert1,tester1); 
    } 

這是有我的Word ADT我的Word文件。它包括一個用於@overide等於

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 Word() { 
     // TODO Auto-generated constructor stub 
    } 

    public int getScore() 
    { 
     return this.score; 
    } 

    public void setScore(int s) 
    { 
     this.score = s; 
    } 

    public String getWord() 
    { 
     return this.word; 
    } 

    public void setWord(Word w) 
    { 
     this.word = w.word; 

    } 

    @Override 
    public int compareTo(Word w) 
    { 
     if (this.score > w.score){ 
      return 1; 
     } 
     if (this.score < w.score){ 
      return -1; 
     } 
     return 0; 
    } 

    @Override 
    public boolean equals(Object x) 
    { 
     if (this == x) { return true; } 
     if (x == null) { return false; } 
     if (getClass() != x.getClass()) { return false; } 
     Word other = (Word) x; 
     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; 
    } 


    public String toString() 
    { 
     //TODO 
     return "{" + this.word + "," + this.score + "}"; 
    } 
} 
+0

凡定義'assertArrayEquals'? – immibis 2015-02-10 22:58:17

+0

它是一個內置的java函數,可以處理int和字符串數組。我只是希望它能與字類型的數組一起工作 – HassaanM 2015-02-10 23:01:24

+0

@immibis這是來自JUnit框架的一種方法。 – 2015-02-10 23:01:26

回答

3

導入靜態方法在org.junit.Assert類:

import static org.junit.Assert.*; 

或使用類名:

Assert.assertArrayEquals(insert1,tester1); 
+0

謝謝你這個作品,我沒有導入靜態org.junit.Assert。*; – HassaanM 2015-02-10 23:03:39

1

如果」重新使用JUnit 4(因爲它看起來像你),你大概不會擴展現有的測試類 - 所以你需要用類名調用靜態assertArrayEquals,例如

import org.junit.Assert; 
... 
Assert.assertArrayEquals(...); 

現在,最後成了一個有點笨重,所以經常開發商靜態導入他們想要的,例如方法

import static org.junit.Assert.assertArrayEquals; 
... 
assertArrayEquals(...); 

或只是導入寄託都:

import static org.junit.Assert.*; 
... 
assertArrayEquals(...); 
相關問題