使用junit测试从文本和二进制文件读取以及写入文本和二进制文件

发布于 12-05 17:49 字数 2432 浏览 2 评论 0原文

我编写了一个 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;
}

}

I wrote a Course class that has constructors that read from text and binary files and has methods to write to text and binary files. How do I write a junit test to test this class?

see code below:

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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书2024-12-12 17:49:21

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

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

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

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

When you're testing constructors you typically only need the following:

  • Ensure that the object created contains the values you expect (i.e. the values you passed in, or the data that results from some computation done on them)
  • The data loaded into the objects matches those in the file (or another InputStream, if you don't want to use actual files in your tests)
  • Exceptions are thrown for invalid input

Other than that, there isn't much to do for constructors. In the case of your first constructor, which simply assigns values (without doing any disk I/O) I don't tend to test those constructors, because all you're really verifying is that the = operator in Java works, which we know is does.

So, I would only write tests that cover the above situations for your second constructor.

×纯※雪2024-12-12 17:49:21

幸运的是,您实际上并没有需要文件 - 因此您可以传入一个写入StringWriterPrintWriter和一个DataInputStream< /code> 包装一个 ByteArrayInputStream 等,并测试内存中的所有内容。

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

Fortunately, you haven't actually required files - so you can pass in a PrintWriter writing to a StringWriter, and a DataInputStream wrapping a ByteArrayInputStream etc, and test everything in memory.

Another option is to have resources in your test project with expected output - always write to memory, but then check the expectations against those "golden" files, which you should probably load using Class.getResource rather than a FileInputStream (to avoid having any file system dependency).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文