我有一个独立的项目来编写测试用例;问题是我无法模拟HttpServletRequest
,因为在我的servlet中有类似getServletContext()
的调用,因为测试用例是从外部servlet容器运行的。它总会返回一个错误,说'&34;没有找到上下文"。这只是servlet容器的一个依赖项;可能有数百个。例如,initialContext.lookup()
也取决于容器。
在这种情况下,如何使用Mockito编写测试用例?请不要询问错误信息;它更像是一个逻辑问题,而不是技术问题。
在互联网上寻找教程让我想知道我是否做了严重的错误。似乎没有人在遇到过这个问题之前......如果没有在servlet中调用HttpServletRequest
,你怎么能模拟getServletContext()
?我的意思是认真的,它有多罕见?
答案 0 :(得分:14)
您有一个使用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来做到这一点。 e.g。
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);
}
}
可以有100个。像initialContext.lookup()也依赖于容器。
在这种情况下,您可以创建一个InitialContext
模拟并使用它。
如果您的代码创建了新的InitialContext
,例如
public void someMethod(){
InitialContext context = new InitialContext();
context.lookup(....);
}
你可以简单地将InitialContext
实例化提取到一个受保护的方法,你可以在测试中覆盖它,就像我上面用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。
修改强>
你可以发布你的Mockito代码吗?这将是一种乐趣,因为这些方法的命名方式不同。
@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);
}