![](/img/trans.png)
[英]How to emulate HttpServletRequest, HttpServletResponse and ServletContext?
[英]how to mock a servletContext instead of Servlet or HttpServletRequest?
我有一個獨立的項目來編寫測試用例; 問題是我不能模擬HttpServletRequest
,只是因為在我的servlet中有像getServletContext()
這樣的調用,因為測試用例是從外部servlet容器運行的。 它總是會返回一個錯誤,說“沒有找到上下文”。 這只是servlet容器的一個依賴項; 可能有數百個。 例如, initialContext.lookup()
也依賴於容器。
在這種情況下,如何使用Mockito編寫測試用例? 請不要詢問錯誤信息; 它更像是一個邏輯問題,而不是技術問題。
在互聯網上尋找教程讓我想知道我是否做了嚴重的錯誤。 似乎沒有人在遇到過這個問題之前......你怎么能在沒有在servlet中調用getServletContext()
情況下模擬HttpServletRequest
? 我的意思是認真的,它有多罕見?
您有一個使用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);
}
}
可以有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
來攔截構造函數 。
編輯
你可以發布你的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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.