【发布时间】:2016-12-21 11:03:35
【问题描述】:
我对 Mockito 的测试有疑问。 我承认我不懂 Mockito。 我阅读了很多页面,阅读了很多示例,但仍然一无所获。
我在 Maven 中有一个程序。我定义文件名。显示文件的内容。 程序为:App(condition and display of ),methodApp(methods)。
应用程序
public static void main(String[] args) throws IOException {
new App();
}
private App() throws IOException {
methodApp ViewProgram = new methodApp();
if (ViewProgram.file == null) {
out.println("No File!");
return;
}
out.println(ViewProgram.removeSpacesDisplaysContents());
}
方法应用
InputStream file = getClass().getResourceAsStream("/"+enterNameFileConsole());
private String enterNameFileConsole(){
out.println("Enter filename:");
try {
return new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException e) {
out.println("Error reading file!");
}
return enterNameFileConsole();
}
String removeSpacesDisplaysContents() {
try {
return deleteWhitespace(new BufferedReader(new InputStreamReader(file)).readLine());
} catch (IOException e) {
out.println("Error reading file!");
}
return removeSpacesDisplaysContents();
}
我必须测试 App()、enterNameFileConsole() 和 removeSpacesDisplaysContents()。
如果有人可以提出和解释或想法,如何使用 Mockito 测试方法和条件。
如果主题重复,请帮助并抱歉。
【问题讨论】:
-
您的程序中有一个无限递归循环,因为方法
removeSpacesDisplaysContents在语句return removeSpacesDisplaysContents();中以无限递归循环调用自身。 -
我尝试更改它,但后来我遇到了输入流问题,没有将文件名提供给存储路径,而是返回时我使用 try ... cry。跨度>
-
您可能应该首先专注于学习 Java 编程的基础知识,确保您彻底了解类和方法的工作原理,然后再学习更复杂的东西,例如 Mockito。
-
我不完全同意。 Ziomell 的代码非常规,并且确实有一个可能的无限循环,但对我来说它表明了理解。也许OP是自学的?无论如何,我试图在不过度重构原始代码的情况下回答这个问题。这对我来说是一个相当大的挑战!