【发布时间】:2017-09-02 13:26:29
【问题描述】:
我们可以为 setAddr() 方法编写测试用例,因为它是私有的 void 方法,请帮助我吗?
class WCfg {
private void setAddr(Cfg cfg, String... arg)
throws Exception {
try {
} catch (Exception e) {
throw new Exception("Invalid IP address.", e);
}
}
public String process(String... arg) throws Exception {
MCfg mCfg = new MCfg();
try {
setAddr(mCfg, arg);
} catch (Exception e) {
return "Wrong argument format.";
}
cfg.write();
return "success";
}
}
【问题讨论】:
-
你不应该为你的私有方法写测试。
-
可以使用reflection访问方法,也可以使用PowerMock
-
提示:下次试试你最喜欢的搜索引擎 ;-)