如何解决运行 JUnit 4 参数化测试时遇到障碍我是 JUnit 测试的新手
我是 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,new Student[] {new Student("E",new Student("F",new Student("G",new Student("H",60)},60}
};
}
}
测试结果如下:
第一个结果:
java.lang.AssertionError: expected:<80> but was:<Student;@78b729e6>
第二个结果:
java.lang.AssertionError: expected:<60> but was:<Student;@7692d9cc>
我现在很困惑这种结果是怎么发生的,请问您对解决这个问题有什么意见吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。