Question

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

}

Was it helpful?

Solution

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.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top