使用junit测试从文本和二进制文件读取以及写入文本和二进制文件
我编写了一个 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 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当您测试构造函数时,通常只需要以下内容:
除此之外,构造函数无需做太多事情。对于第一个构造函数,它只是分配值(不执行任何磁盘 I/O),我不倾向于测试这些构造函数,因为您真正要验证的是
=
运算符在 Java 中是有效的,我们知道是这样的。因此,我只会为第二个构造函数编写涵盖上述情况的测试。
When you're testing constructors you typically only need the following:
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.