为作业创建测试,我得到一个奇怪的AssertionError例外.
我改变它直到我得到一个简单的案例:
List<Integer> elements= new ArrayList<Integer>();
elements.add(1);
elements.add(2);
elements.add(3);
Permutation p2 = new Permutation(elements);
Permutation p1 = new Permutation(elements);
assertThat(p2, equalTo(p1));
Run Code Online (Sandbox Code Playgroud)
Permutation.java:
public class Permutation {
private List<Integer> elements;
public Permutation(List<Integer> elements) {
this.elements = elements;
}
public boolean equals(Permutacion permutation){
if ( this.elements.size() != permutation.elements.size()){
return false;
}
Iterator<Integer> iterThis = this.elements.iterator();
Iterator<Integer> iterOther = permutation.elements.iterator();
while ( iterThis.hasNext() && iterOther.hasNext()){
if ( iterThis.next() != iterOther.next()){
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
在junit和hamcrest源代码中挖掘我发现junit assert只在匹配器上调用匹配器.
这种情况下的匹配方法是:
public …Run Code Online (Sandbox Code Playgroud)