你好专家, 我想启动Junit / mockito等测试框架,因为我已经看到很多博客,甚至在这里人们都建议编写测试用例而不是sysout和所有..但我一直在努力测试:-( .. 我经历了教程,并且大部分教程都展示了进行求和,除法,加法等测试的例子。好吧我明白了但是我如何在现实生活中使用它。有人可以指导我并帮助编写测试用例,例如以下VOID方法:
public void callHtmls(List<String> pathList, Session session, Image img){
Iterator<String> it = pathList.iterator();
while(it.hasNext()){
String path = it.next();
String htmlPath = getHtmlPath(path); // call to a method
if(htmlPath!=null){
System.out.println("HTML Path is = " + htmlPath);
try {
callImagePrinter(htmlPath,session, img); //again call to a method
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
请帮帮我!!!!!由于测试,我正在做一场噩梦: - (
答案 0 :(得分:0)
首先找到依赖项:
然后定义您要测试的内容。
示例:
使用Mockito存储输入:
YourObject spy = spy(yourObject);
when(spy.getHtmlPath()).thenReturn("htmlpath");
然后调用你的方法:
callHtmls(asList("html1","html2"), null, null);
并验证callImagePrinter的输出:
verify(spy).callImagePrinter(eq("htmlpath"), any(Session.class), any(Image.class));
请注意,spy
,when
,eq
和any
是Mockito
的静态方法。