所以我正在阅读一些教程,例如:click。
但是我仍然不知道如何测试我的函数是否创建了正确的文件。这是我的代码:
public void deleteDirectories(List<GroupOfCountries> organizedCountries, String path) {
DirectoriesActivity userDecision = UserInputs.userDecisionAboutDirectories();
List<String> listOfThreeLettersGroups = new ArrayList<String>();
if (userDecision == DirectoriesActivity.DELETE) {
listOfThreeLettersGroups = GroupOfCountries.returnLettersGroups();
} else if (userDecision == DirectoriesActivity.REPLACE) {
for (GroupOfCountries groupedCountries : organizedCountries) {
listOfThreeLettersGroups.add(groupedCountries.getName());
}
}
if ((userDecision == DirectoriesActivity.DELETE) || (userDecision == DirectoriesActivity.REPLACE)) {
for (String directoryToDelete : listOfThreeLettersGroups) {
String pathOfGorupDirectory = (path + File.separator + directoryToDelete);
File tempfile = createFile(pathOfGorupDirectory);
deleteDirectory(tempfile);
}
}
}
protected File createFile(String pathOfGorupDirectory) {
return new File(pathOfGorupDirectory);
}
我需要测试createFile函数是否返回正确的File对象。有人能帮我吗?我是Mockito的全新人,从来没有在其中制作任何东西。我试着这样开始:
public class SomeClassTest {
SomeClass deleter;
@Before
public void setupMock(){
deleter = mock(SomeClass.class);
deleter.deleteDirectories(/*List*/, /*path*/);
}
@Test
public void test() {
}
}
@edit
我编辑了我的代码,也许现在看起来会更好。
@ EDIT2
输入和输出:
listOfThreeLettersGroups = ABC, DEF, GHI, JKL, MNO, ...
organizedCountries = Albania, Czech Republic, Poland, Portugal...
path = /home/test
So for example:
pathOfGroupDirectory = /home/test/ABC
这里也是“删除”功能:
private static 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();
}
我想测试准备删除的文件夹是否为corect。 (例如,如果createFile返回File(“/ home / test / ABC”))。