Java 如何使用mockito检查是否删除了正确的文件
我有一个程序,为给定国家列表中的每个条目创建一个目录。 例如:如果您传递阿尔巴尼亚、葡萄牙、比利时、以色列的列表,它将创建一个目录ABC(创建阿尔巴尼亚和比利时的目录)、PQR(创建葡萄牙的目录)、GHI(创建以色列的目录)。如果该程序在博茨瓦纳和波兰再次运行,它将删除目录ABC和PQR,然后重新创建它们 我想写一个测试,验证只有目录ABC和PQR被删除,而目录GHI仍然存在。这个程序运行正常,但是现在我需要编写一个mockito测试,我不知道怎么做。我对mockito是新来的:/这是我的课程Java 如何使用mockito检查是否删除了正确的文件,java,mockito,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();
}
}
公共类FileDeleter实现Deleter{
public void deleteDirectory(列出organizedCountries,字符串路径){
List ListOfTheilettersGroups=new ArrayList();
适用于(国家组国家组:有组织国家){
ListOfTheilettersGroups.add(groupedCountries.getName());//这里它在ArrayList中添加了“ABC”和“PQR”,因为我的国家是阿尔巴尼亚、比利时和葡萄牙。
}
对于(String directoryToDelete:ListOfTheilettersGroups){
字符串pathOfGorupDirectory=(path+File.separator+directoryToDelete);//这里它正在创建指向ABC和PQR目录的路径,例如/home/test/ABC
File tempfile=createFile(pathOfGorupDirectory);
删除目录(tempfile);
}
}
受保护的文件createFile(字符串PathFGorUpDirectory){
返回新文件(pathOfGorupDirectory);
}
私有布尔删除目录(文件目录){
if(dir.isDirectory()){
File[]children=dir.listFiles();
for(int i=0;i
这是我的测试。我试图用这种方式来解决这个问题,但我不确定我还能在这里做些什么:/
@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();
}
}
@RunWith(MockitoJUnitRunner.class)
公共类FileDeleterTest{
@嘲弄
私有文件;
@嘲弄
私有文件子文件;
私人文件删除器文件删除器;
@以前
公共作废设置(){
fileDeleter=新的fileDeleter(用户输入){
受保护的文件createFile(字符串PathFGorUpDirectory){
返回文件;
};
};
}
@试验
public void testMultipleGroupsReplace(){
File[]fileChildrenEmpty=新文件[0];
文件[]fileChildrenWithMockChild=新文件[]{childFile};
doReturn(true).doReturn(false).when(file).isDirectory();
doReturn(fileChildrenWithMockChild).doReturn(fileChildrenEmpty).when(file).listFiles();
String path=“somepath”;
List groupOfCountries=new ArrayList();
列表国家=新的ArrayList();
添加(新国家(“阿尔巴尼亚”);
添加(新国家(“比利时”);
国家组第一组=新国家组(“ABC”,国家);
国家组。添加(第一组);
国家/地区=新阵列列表();
添加(新国家(“葡萄牙”);
国家组第二组=新国家组(“PQR”,国家);
国家组。添加(第二组);
fileDeleter.deleteDirectory(国家组,路径);
验证(childFile,times(1)).delete();
}
}
创建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;
}
}
我不会用Mockito做这个测试。只要使用真正的文件系统,我必须使用Mockito。我需要学习如何使用它,我需要在这个示例中学习它。例如,是否有任何方法可以从deleteDirectory方法返回dir.getName()?
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();
}
}
Mockito.verify(mockFileDeleter).when(deleteDirectory("directory name to verify"));