我在java中遇到了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文件,它有我的Word ADT。它包括@overide for equals
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 :(得分:3)
导入org.junit.Assert类中的静态方法:
import static org.junit.Assert.*;
或使用班级名称:
Assert.assertArrayEquals(insert1,tester1);
答案 1 :(得分:1)
如果你正在使用JUnit 4(因为它看起来像你),你可能没有扩展现有的测试类 - 所以你需要用类名调用静态assertArrayEquals
,例如
import org.junit.Assert;
...
Assert.assertArrayEquals(...);
现在,这最终变得有点笨拙,所以开发人员通常会静态导入他们想要的方法,例如。
import static org.junit.Assert.assertArrayEquals;
...
assertArrayEquals(...);
或只是导入everthing:
import static org.junit.Assert.*;
...
assertArrayEquals(...);