我知道你不能使用mockito来模拟静态方法.但我试图模拟的方法不是静态的,而是在其中调用静态方法.那我可以嘲笑这个方法吗?
我在运行测试时遇到异常.调用静态方法是否有这种异常的原因?
要测试的类:
public class MyAction{
public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
MyService service = new MyService();
**form.setList(service.getRecords(searchRequest));**
}
}
Run Code Online (Sandbox Code Playgroud)
模拟类和方法:
public class MyService{
public List getRecords(SearchRequest sr){
List returnList = new ArrayList();
MyDao dao = DAOFactory.getInstance().getDAO();---->Call to static method
// Some codes
return returnList;
}
}
Run Code Online (Sandbox Code Playgroud)
使用静态方法的类:
public class DAOFactory{
private static DAOFactory instance = new DAOFactory();
public static DAOFactory getInstance() {------> The static method
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试:
@Test
public …Run Code Online (Sandbox Code Playgroud) 我们正在接管来自不同团队的申请.我们只有5天的时间来学习这个团队应用程序的所有可能性.这五天后,这个原始团队将无法使用.
此时,我们甚至不知道应用程序的大小或复杂性.我们所知道的是它是一个j2ee应用程序.我猜这是一个相当大的应用程序.过去几年我一直是开发人员,我以前从未做过这样的事情.所以我甚至不确定从哪里开始.
我的问题包括但不限于: