我有一个独立的项目来编写测试用例;问题是我无法模拟HttpServletRequest
,这仅仅是因为在我的servlet中有像getServletContext()
这样的调用,因为测试用例是从外部servlet容器中运行的。它将始终返回一个错误,指出“未找到上下文”。这只是servlet容器的一种依赖关系。可能有数百个。例如,initialContext.lookup()
也取决于容器。
在这种情况下,如何使用Mockito编写测试用例?请不要询问错误消息;这更多的是逻辑上的问题,而不是技术上的问题。
在互联网上查找教程使我想知道我是否在做严重错误的事情。似乎没有人以前遇到过这个问题。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。我的意思是说,这有多少稀有?
最佳答案
您有一个使用ServletContext
的servlet实现,例如
public class SomeServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
Object attribute = servletContext.getAttribute("test");
System.out.println(attribute.toString());
}
}
在这种情况下,您有2个选项可以测试
doGet
方法使用powermock的partitial mocking仅模拟
getServletContext
方法。@RunWith(PowerMockRunner.class)
public class SomeServletTest {
@Test
public void onGet() throws ServletException, IOException{
SomeServlet someServlet = PowerMock.createPartialMock(SomeServlet.class, "getServletContext");
ServletContext servletContext = PowerMock.createNiceMock(ServletContext.class);
HttpServletRequest httpServletRequest = PowerMock.createNiceMock(HttpServletRequest.class);
HttpServletResponse httpServletResponse = PowerMock.createNiceMock(HttpServletResponse.class);
someServlet.getServletContext();
PowerMock.expectLastCall().andReturn(servletContext);
servletContext.getAttribute("test");
PowerMock.expectLastCall().andReturn("hello");
PowerMock.replay(someServlet, servletContext, httpServletRequest, httpServletResponse);
someServlet.doGet(httpServletRequest, httpServletResponse);
}
}
或更简单的方法是仅覆盖
getServletContext
方法。在这种情况下,您不需要powermock。您可以使用easymock做到这一点。例如public class SomeServletTest {
@Test
public void onGet() throws ServletException, IOException{
HttpServletRequest httpServletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
HttpServletResponse httpServletResponse = EasyMock.createNiceMock(HttpServletResponse.class);
final ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
SomeServlet someServlet = new SomeServlet(){
public ServletContext getServletContext() {
return servletContext; // return the mock
}
};
EasyMock.expect(servletContext.getAttribute("test")).andReturn("hello");
EasyMock.replay(servletContext, httpServletRequest, httpServletResponse);
someServlet.doGet(httpServletRequest, httpServletResponse);
}
}
在这种情况下,您可以创建一个
InitialContext
模拟并使用它。如果您的代码创建了新的
InitialContext
,例如public void someMethod(){
InitialContext context = new InitialContext();
context.lookup(....);
}
您可以简单地将
InitialContext
实例提取到一个 protected 方法,您可以在测试中覆盖该方法,就像我上面用ServletContext
显示的那样public void someMethod(){
InitialContext context = createInitialContext();
context.lookup(....);
}
protected InitialContext createInitialContext(){
return new InitialContext(); // can be overridden by a subclass
// and therefore by tests as well to
// return a mock
}
如果您不希望或者无法以这种方式修改代码,则可以使用
Powermock
到intercept the constructor。编辑
@Test
public void onGet() throws ServletException, IOException {
HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class);
final ServletContext servletContext = Mockito.mock(ServletContext.class);
SomeServlet someServlet = new SomeServlet(){
public ServletContext getServletContext() {
return servletContext; // return the mock
}
};
Mockito.doReturn("hello").when(servletContext).getAttribute("test");
someServlet.doGet(httpServletRequest, httpServletResponse);
}