测试一个向前重定向的无效方法

Test a void method that redirect foward

如何测试使用 RequestDispatcher 重定向我的无效方法?

到目前为止我做了什么。

public void testAuthAction_userNull() {
    HttpServletRequest requestMock = createMock(HttpServletRequest.class);
    HttpServletResponse responseMock = createMock(HttpServletResponse.class);
    expect(requestMock.getSession().getAttribute("user")).andReturn(null);
    replay(requestMock);

    AuthAction action = new AuthAction();
    RequestDispatcher rd = requestMock.getRequestDispatcher("/User/login.jsp");
}

我要测试的方法是

public void execute(HttpServletRequest request, HttpServletResponse response) {
    User user = (User) request.getSession().getAttribute("User");
    try {
        if(user == null) {
            RequestDispatcher rd = request.getRequestDispatcher("/User/login.jsp");
            if(rd != null)
                rd.foward(request, response);
        } else {/* */}
    }
    catch(Exception e){/* */}
}

我正在使用 JUnit 和 EasyMock。

您需要创建一个模拟 RequestDispatcher 期望被转发,并且 return 它来自您的模拟:

RequestDispatcher dispatcherMock = createMock(RequestDispatcher.class);
expect(requestMock.getRequestDispatcher("/User/login.jsp"))
    .andReturn(dispatcherMock);
// Expect to be forwarded.
dispatcherMock.forward(requestMock, responseMock);
EasyMock.expectLastCall().once();
replay(dispatcherMock);
replay(requestMock);

// Run your test on whatever instance has `execute`:
someInstance.execute(requestMock, responseMock);

我会提供一个我认为应该有帮助的长答案。

所以,测试的方法是这样的。

public void execute(HttpServletRequest request, HttpServletResponse response) {
    User user = (User) request.getSession().getAttribute("User");
    try {
        if(user == null) {
            RequestDispatcher rd = request.getRequestDispatcher("/User/login.jsp");
            if(rd != null)
                rd.forward(request, response);
        } else {/* */}
    }
    catch(Exception e){/* */}
}

一个可行的测试方法是这样的:

@Test
public void testAuthAction_userNull() {
    HttpServletRequest requestMock = mock(HttpServletRequest.class);
    HttpServletResponse responseMock = mock(HttpServletResponse.class);
    HttpSession sessionMock = mock(HttpSession.class);

    expect(requestMock.getSession()).andReturn(sessionMock);
    expect(sessionMock.getAttribute("User")).andReturn(null);
    expect(requestMock.getRequestDispatcher("/User/login.jsp")).andReturn(null);

    replay(requestMock, sessionMock);

    execute(requestMock, responseMock);

    verify(requestMock, sessionMock);
}

我正在使用 mock() 而不是 createMock()。它是一样的,但更好更短。

它 returns 是一个空的调度程序,因为不需要更多。我添加了 verify() 以确保一切都按预期调用。

然后,如果你想确保 forward 也被调用,你还需要 RequestDispatcher.

的模拟
@Test
public void testAuthAction_userNull() throws Exception {
    HttpServletRequest requestMock = mock(HttpServletRequest.class);
    HttpServletResponse responseMock = mock(HttpServletResponse.class);
    HttpSession sessionMock = mock(HttpSession.class);
    RequestDispatcher rdMock = mock(RequestDispatcher.class);

    expect(requestMock.getSession()).andReturn(sessionMock);
    expect(sessionMock.getAttribute("User")).andReturn(null);
    expect(requestMock.getRequestDispatcher("/User/login.jsp")).andReturn(rdMock);

    rdMock.forward(requestMock, responseMock);

    replay(requestMock, sessionMock, rdMock);

    execute(requestMock, responseMock);

    verify(requestMock, sessionMock, rdMock);
}

verify() 将确保调用 forward()。您不需要 expectLastCall()。它是隐式的。

那么为了简化,我实际上会这样做:

public class MyTest extends EasyMockSupport {
    @Test
    public void testAuthAction_userNull() throws Exception {
        HttpServletRequest requestMock = mock(HttpServletRequest.class);
        HttpServletResponse responseMock = mock(HttpServletResponse.class);
        HttpSession sessionMock = mock(HttpSession.class);
        RequestDispatcher rdMock = mock(RequestDispatcher.class);

        expect(requestMock.getSession()).andReturn(sessionMock);
        expect(sessionMock.getAttribute("User")).andReturn(null);
        expect(requestMock.getRequestDispatcher("/User/login.jsp")).andReturn(rdMock);

        rdMock.forward(requestMock, responseMock);

        replayAll();

        execute(requestMock, responseMock);

        verifyAll();
    }
}

EasyMockSupport class 使代码更简单。

老实说,在这种情况下,当使用 Spring 时,我会使用 spring-test.

@Test
public void testAuthAction_userNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    execute(request, response);

    assertThat(response.getForwardedUrl()).isEqualTo("/User/login.jsp");
}

它做的事情完全相同,但如您所见,它要短得多,因为会话和请求调度程序是在幕后创建的,其行为与您期望的一样。