如何更改模拟中的布尔值

时间:2015-08-17 10:39:29

标签: java mockito

所以这是我的班级:

public class FileDeleter implements Deleter {

    public void deleteDirectories(List<GroupOfCountries> organizedCountries, String path) {
        List<String> listOfThreeLettersGroups = new ArrayList<String>();

        for (GroupOfCountries groupedCountries : organizedCountries) {
            listOfThreeLettersGroups.add(groupedCountries.getName()); //Here it's adding "ABC" and "PQR" to ArrayList because my countries are Albania, Belgium and Portugal.
        }

        for (String directoryToDelete : listOfThreeLettersGroups) { 
            String pathOfGorupDirectory = (path + File.separator + directoryToDelete); //Here it's creating paths to ABC and PQR directories, for example /home/test/ABC
            File tempfile = createFile(pathOfGorupDirectory);
            deleteDirectory(tempfile);
        }
    }

    protected File createFile(String pathOfGorupDirectory) {
        return new File(pathOfGorupDirectory);
    }

    private boolean deleteDirectory(File dir) {
        if (dir.isDirectory()) {
            File[] children = dir.listFiles();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDirectory(children[i]);
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }
}

我希望100%错过分支机构。如果我评论这些行:

if (!success) {
   return false;
}

我有100%错过了分支机构。但有人知道我可以用mockito / junits将成功转化为假吗?因为成功总是返回true,所以这永远不会发生。

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助。

FileDeleter deleter = Mockito.mock(FileDeleter.class);
Mockito.when(deleter.deleteDirectory(Mockito.any())).thenReturn(false);