jeudi 12 novembre 2015

How to mock a method that returns a file

In my unit testing, I want to mock a method which returns a file in java. How do I mock a method that returns a file in java? The code looks something along the lines of

public File getFile(String userName, String hostName)
            throws VerifyException, IOException {
        hostName = "/" + hostName;
        userName = "/" + userName;
        baseDirectory = new File(hostName, userName);
        if (!baseDirectory.exists()) {
            baseDirectory.mkdirs();
        }
        fileName = new SimpleDateFormat("yyyyMMddhhmmssSSS'.txt'").format(new Date());
        file = new File(baseDirectory, fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

Aucun commentaire:

Enregistrer un commentaire