【问题标题】:Use junit to test reading from text and binary files and writing to text and binary files使用 junit 测试读取文本和二进制文件以及写入文本和二进制文件
【发布时间】:2011-09-19 14:58:46
【问题描述】:

我编写了一个 Course 类,它具有从文本和二进制文件读取的构造函数,并具有写入文本和二进制文件的方法。如何编写一个 junit 测试来测试这个类?

见下面的代码:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Course {
//instance variables
private String courseID;
private String courseName;
private int numberOfCredits;
private String departmentID;

public Course(String courseID,String courseName, int numberOfCredits, String departmentID){
    //constructor
    this.courseID=courseID;
    this.courseName=courseName;
    this.numberOfCredits=numberOfCredits;
    this.departmentID=departmentID;
}

public Course(Scanner inputFile)throws Exception{
    //constructor, read data from text file
    try{
        courseID=inputFile.nextLine();
        courseName=inputFile.next();
        numberOfCredits=inputFile.nextInt();
        departmentID=inputFile.next();
    }
    catch(Exception e){
        throw e;
    }

}

public Course (DataInputStream binFile)throws Exception{
    //constructor reads from binary file and assign values to variables
    try{
    courseID=binFile.readUTF();
    courseName=binFile.readUTF();
    numberOfCredits=binFile.readInt();
    departmentID=binFile.readUTF();
    }
    catch(Exception e){
        throw e;
    }
}
public void saveToTextFile(PrintWriter file){
    //prints to text file
    file.printf(" %s %s %d %s ", courseID, courseName, numberOfCredits, departmentID);
}
public void saveToBin(DataOutputStream binFile)throws Exception{
    //saves information to binary file
    binFile.writeUTF(courseID);
    binFile.writeUTF(courseName);
    binFile.writeInt(numberOfCredits);
    binFile.writeUTF(departmentID);
}



public String toString(){
    //setup string for course display
    String info=courseID + " " + courseName+ " "+ numberOfCredits+" "+ departmentID;
    return info;
}

//getters and setters
public String getCourseName() {
    return courseName;
}

public void setCourseName(String courseName) {
    this.courseName = courseName;
}

public int getNumberOfCredits() {
    return numberOfCredits;
}

public void setNumberOfCredits(int numberOfCredits) {
    this.numberOfCredits = numberOfCredits;
}

public String getDepartmentID() {
    return departmentID;
}

public void setDepartmentID(String departmentID) {
    this.departmentID = departmentID;
}

public String getCourseID() {
    return courseID;
}

}

【问题讨论】:

    标签: java junit dataoutputstream datainputstream


    【解决方案1】:

    当您测试构造函数时,您通常只需要以下内容:

    • 确保创建的对象包含您期望的值(即您传入的值,或对它们进行的某些计算产生的数据)
    • 加载到对象中的数据与文件中的数据相匹配(或另一个 InputStream,如果您不想在测试中使用实际文件)
    • 无效输入引发异常

    除此之外,构造函数没有什么可做的。对于您的第一个构造函数,它只是分配值(不进行任何磁盘 I/O),我不倾向于测试这些构造函数,因为您真正要验证的是 Java 中的 = 运算符是否有效,这我们知道是的。

    所以,我只会为您的第二个构造函数编写涵盖上述情况的测试。

    【讨论】:

    • 测试保存到文本和二进制文件怎么样?
    • 取决于您的情况,但是在测试保存/加载时,我通常会测试以确保 (1) 我可以保存而不会引发错误,以及 (2) 我可以加载已保存的文件通过我自己的代码,加载到生成的 Java 对象中的值看起来正确且有效。它不必很复杂——您只需确保从应用程序到磁盘再返回到应用程序的往返行程以从一次运行到下一次运行的一致形式保留您的数据。所以当保存一个整数值'5'时,当我从磁盘加载它时,它最好还是等于'5'。
    【解决方案2】:

    幸运的是,您实际上并没有必需文件-因此您可以将PrintWriter 写入StringWriterDataInputStream 包装ByteArrayInputStream 等,然后进行测试一切都在记忆中。

    另一种选择是在您的测试项目中使用 expected 输出的资源 - 始终写入 到内存中,然后检查您对那些“黄金”文件的期望应该可能使用Class.getResource 而不是FileInputStream 加载(以避免任何文件系统依赖)。

    【讨论】:

    • 你能详细说明一下我是新手
    • @trs:我添加了更多信息,但如果您能说出您遇到的问题,这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多