测试写入文件和不带参数从文件读取

Test write to file and read from file without parameters

我想测试从文件读取和写入文件的功能。我的问题是函数没有任何参数。到目前为止,我只用文件作为参数测试过这种功能。我查看了其他问题并找到了这些:How to unit test a method that reads a given file, How to test write to file in Java? 但它们并没有真正帮助我。我要测试的代码是:

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

谁能告诉我如何在没有文件参数的情况下进行测试?

如果您无法更改现有方法的签名,则添加一个接受参数的新方法,并将现有方法更改为委托给新方法。

@Deprecated 
public void loadData() {
    loadData(super.fileName);
}

public void loadData(String fileName) {
    // do stuff
}

@Deprecated
public synchronized void writeToFile() {
    writeToFile(super.fileName);
}

public synchronized void writeToFile(String fileName) {
    // do stuff
}