如何使用mockito检查是否删除了正确的文件

时间:2015-08-13 11:42:54

标签: java mockito

我有一个程序,它为给定国家/地区列表中的每个条目创建一个目录。 例如:如果你通过阿尔巴尼亚,葡萄牙,比利时,以色列的名单,它将创建一个目录ABC(创建目录阿尔巴尼亚和比利时),PQR(创建葡萄牙目录),GHI(以色列目录所在的目录)创建)。如果程序再次与博茨瓦纳和波兰一起运行,它将删除目录ABC和PQR,然后再次重新创建它们。

我想编写一个测试,验证只删除目录ABC和PQR,并保留目录GHI。该程序运行正常,但现在我需要编写一个mockito测试,我不知道该怎么做。我是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();
    }
}

这是我的考验。我试着这样做,但我不确定我能在这做什么:/

@RunWith(MockitoJUnitRunner.class)
public class FileDeleterTest {

    @Mock
    private File file;

    @Mock
    private File childFile;

    private FileDeleter fileDeleter;

    @Before
    public void setUp() {
        fileDeleter = new FileDeleter(userInputs) {
            protected File createFile(String pathOfGorupDirectory) {
                return file;
            };
        };
    }

    @Test
    public void testMultipleGroupsReplace() {

        File[] fileChildrenEmpty = new File[0];
        File[] fileChildrenWithMockChild = new File[] { childFile };
        doReturn(true).doReturn(false).when(file).isDirectory();
        doReturn(fileChildrenWithMockChild).doReturn(fileChildrenEmpty).when(file).listFiles();
        String path = "somepath";

        List<GroupOfCountries> groupOfCountries = new ArrayList<GroupOfCountries>();
        List<Country> countries = new ArrayList<Country>();
        countries.add(new Country("Albania"));
        countries.add(new Country("Belgium"));
        GroupOfCountries firstGroup = new GroupOfCountries("ABC", countries);
        groupOfCountries.add(firstGroup);

        countries = new ArrayList<Country>();
        countries.add(new Country("Portugal"));
        GroupOfCountries secondGroup = new GroupOfCountries("PQR", countries);
        groupOfCountries.add(secondGroup);

        fileDeleter.deleteDirectories(groupOfCountries, path);

        verify(childFile, times(1)).delete();
    }
}

1 个答案:

答案 0 :(得分:1)

您创建FileDeleter类的方式很难测试。我建议您将文件操作移动到单独的类/接口,以便您可以使用Mockito模拟其行为。你的课程可能是这样的。

public class CountryDirectoryDeleter {

    private Deleter fileDeleter;

    public CountryDirectoryDeleter() {
        fileDeleter = new FileDeleter();
    }

    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 = fileDeleter.createFile(pathOfGorupDirectory);
            fileDeleter.deleteDirectory(tempfile);
        }
    }


    public FileDeleter getFileDeleter() {
        return fileDeleter;
    }

    public void setFileDeleter(FileDeleter fileDeleter) {
        this.fileDeleter = fileDeleter;
    }
}

FileDeleter看起来像这样:

public class FileDeleter implements Deleter {

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

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

然后在您的测试中,您可以设置&#39; fileDeleter&#39; CountryDirectoryDe​​leter中的字段是一个模拟对象,在测试中,您可以验证是否使用正确的文件名调用了模拟对象的deleteDirectory()方法。

Mockito.verify(mockFileDeleter).when(deleteDirectory("directory name to verify"));