微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

运行 JUnit 4 参数化测试时遇到障碍我是 JUnit 测试的新手

如何解决运行 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?