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));**
}
}
モックされたクラスとメソッド:
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;
}
}
静的メソッドを持つクラス:
public class DAOFactory{
private static DAOFactory instance = new DAOFactory();
public static DAOFactory getInstance() {------> The static method
return instance;
}
}
私のテスト:
@Test
public void testSearch() throws Exception{
MyService service = mock(MyService.class);
MyAction action = new MyAction();
List<MyList> list = new ArrayList<MyList>();
when(service.getRecords(searchRequest)).thenReturn(list);
ActionForward forward = action.search(mapping, form, request, response);
}
これは、テストを実行したときのスタックトレースです。わかりやすくするために、クラスの名前を変更したことに注意してください。