我是 JUnit 测试的新手。我在这个参数化测试中遇到了一个问题,即使我尝试了一些方法,测试也总是失败。
import java.util.ArrayList;
class Student {
String name;
int mark;
public Student(String name, int mark) {
this.name = name;
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (mark != other.mark)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class CheckStudentClass {
public Student[] checkForHighestMarks(int minMark, int numStudents, Student[] studArray) {
ArrayList<Student> studList = new ArrayList<>();
int currentHighestMark = 0;
for (int i = 0; i < numStudents; i++) {
if (minMark < studArray[i].getMark()) {
if (currentHighestMark < studArray[i].getMark()) {
studList = new ArrayList<>();
currentHighestMark = studArray[i].getMark();
studList.add(studArray[i]);
}
else if (currentHighestMark == studArray[i].getMark()) {
studList.add(studArray[i]);
}
}
}
Student[] arrayToReturn = new Student[studList.size()];
arrayToReturn = studList.toArray(arrayToReturn);
return arrayToReturn;
}
}
这是测试类:
package my.edu.utar;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import junitparams.Parameters;
import junitparams.JUnitParamsRunner;
import org.junit.Test;
@RunWith(JUnitParamsRunner.class)
public class CheckStudentClassTest {
@Test
@Parameters(method = "marksData")
public void testCheckForHighestMarks(int aMinMark, int aStudentNumber, Student[] aStudentArray,
int expectedResult) {
CheckStudentClass csc = new CheckStudentClass();
Student[] actualResult = csc.checkForHighestMarks(aMinMark, aStudentNumber, aStudentArray);
assertEquals(expectedResult, actualResult);
}
public Object[] marksData() {
return new Object[] {
new Object[] {50, 4, new Student[] {new Student("A", 50), new Student("B", 60), new Student("C", 70), new Student("D", 80)}, 80},
new Object[] {50, 4, new Student[] {new Student("E", 60), new Student("F", 60), new Student("G", 60), new Student("H", 60)}, 60}
};
}
}
这是测试结果:
第一个结果:
java.lang.AssertionError: expected:<80> but was:<Student;@78b729e6>
第二个结果:
java.lang.AssertionError: expected:<60> but was:<Student;@7692d9cc>
我现在很困惑这种结果是如何发生的,请问您有什么意见可以解决这个问题?