如何模拟servletContext而不是Servlet或HttpServletRequest?

Viv*_*vek 9 java unit-testing mockito java-ee

我有一个独立的项目来编写测试用例; 问题是我无法模拟HttpServletRequest,只是因为在我的servlet中有一些调用就像getServletContext()测试用例是从外部servlet容器运行的.它总是会返回一个错误,说"没有找到上下文".这只是servlet容器的一个依赖项; 可能有数百个.例如,initialContext.lookup()还取决于容器.

在这种情况下,如何使用Mockito编写测试用例?请不要询问错误信息; 它更像是一个逻辑问题,而不是技术问题.

在互联网上寻找教程让我想知道我是否做了严重的错误.似乎没有人在遇到过这个问题之前......你怎么能在HttpServletRequest没有getServletContext()调用servlet的情况下进行模拟?我的意思是认真的,它有多罕见?

Ren*_*ink 15

您有一个使用的servlet实现ServletContext,例如

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());
   }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您有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);
    }
}
Run Code Online (Sandbox Code Playgroud)

或者更简单的方法就是覆盖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);
    }
}
Run Code Online (Sandbox Code Playgroud)

可以有100个.像initialContext.lookup()也依赖于容器.

在这种情况下,您可以创建一个InitialContext模拟并使用它.如果您的代码创建了一个新的InitialContext,例如

public void someMethod(){
    InitialContext context = new InitialContext();
    context.lookup(....);
}
Run Code Online (Sandbox Code Playgroud)

你可以简单地将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
}
Run Code Online (Sandbox Code Playgroud)

如果你不想或者不能用这种方式修改代码,那么你可以使用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);
}
Run Code Online (Sandbox Code Playgroud)