I want to test a read from file and a write to file functions. My problem is that the functions don;t have any parameters. Until now i've tested this kind of fucntions only with the file as parameter. I've looked through other questins and found these: How to unit test a method that reads a given file, How to test write to file in Java? but they didn't really help me. The code i want ot test is:
public void loadData()
{
try(BufferedReader br=new BufferedReader(new FileReader(super.fileName))){
String line;
while((line=br.readLine())!=null){
String[] atributes=line.split(";");
if(atributes.length!=4)
throw new Exception("Linia nu este valida!");
Student t=new Student(atributes[0],atributes[1],atributes[2],atributes[3]);
super.save(t);
}
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void writeToFile() {
try (BufferedWriter br = new BufferedWriter(new FileWriter(super.fileName))){
super.entities.forEach(x -> {
try {
br.write(x.studentFileLine());
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
Can anybody give me a hint about how to test this without the file parameter?