I have a static method that writes serialized object. How can I write JUnit tests to see if it works? Here is my method…
public static void writeToDisk(Data data){
try{
FileInputStream fis = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(fis);
dataList = (DataList) in.readObject();
in.close();
}
catch(Exception e){
dataList = new DataList();
System.out.println("New file created!");
}
dataList.insertData(data);
try{
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(dataList);
out.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
Start with no file.
Call the method with test
Dataargument. Then read it back from resulting file (pull data from list as needed) and compare to original.Repeat with new objects to see if new data is added successfully.