gpt4 book ai didi

java - 如何使用mockito更改函数中的 boolean 值

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:24 25 4
gpt4 key购买 nike

这是我的类(class):

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 将 success 变为 false 做什么?因为 success 总是返回 true,所以这种情况永远不会发生。

最佳答案

也许这会有所帮助。

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

关于java - 如何使用mockito更改函数中的 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32048290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com