EasyMock + Spring的Mock

本来想模拟JSF的 FacesContext.getCurrentInstance().getExternalContext().getRequest()来获得Principal 但发现比较麻烦,所以只实现模拟HttpServletRequest

使用EasyMock + Spring的MockObject做较为简单

1
2
3
4
5
6
7
8
Principal principal = EasyMock.createMock(Principal.class);
expect(principal.getName()).andReturn("JUDYEUNG");
MockHttpServletRequest request = new MockHttpServletRequest();
replay(principal);

request.setUserPrincipal(principal);

uploadBean.setRequest(request);