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

标签 java unit-testing jakarta-ee mockito

我有一个编写测试用例的独立项目;问题是我不能模拟 HttpServletRequest,因为在我的 servlet 中有像 getServletContext() 这样的调用,因为测试用例是从外部 servlet 容器运行的。它总是会返回一个错误,说“找不到上下文”。这只是 servlet 容器的一个依赖项;可以有数百个。例如,initialContext.lookup() 也依赖于一个容器。

这种场景下如何使用Mockito编写测试用例?请不要询问错误信息;这与其说是技术问题,不如说是逻辑问题。

在互联网上寻找教程让我想知道我是否做错了什么。之前似乎没有人遇到过这个问题......你怎么能模拟 HttpServletRequest 而没有在 servlet 中调用 getServletContext() ?我是说真的,它能有多罕见?

最佳答案

您有一个使用 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);
    }
}

there can be 100's. like initialContext.lookup() also dependent on container.

在这种情况下,您可以创建一个 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
}

如果你不想或者不能这样修改代码,那么你可以使用Powermockintercept the constructor .

编辑

Can you post your Mockito code? It would be a pleasure, since the methods are named differently.

@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);
}

关于java - 如何模拟 servletContext 而不是 Servlet 或 HttpServletRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22610503/

相关文章:

java - 扫描蓝牙设备

java - final 字段的线程安全

java - 使用 gradle 为各个单元测试生成 Jacoco 代码覆盖率报告

java - 更新 Java EE 7 CDI 应用程序中注入(inject)的域对象

java - JSP 不抛出 NullPointerException

java - 不完整的@JoinColumns

python - 单元测试请求重试python

c# - 出于单元测试目的,如何更改 "today' 的日期?

spring - 将典型的 3 层架构转移给参与者

java - 如何定义JPA?