模拟注销会引发错误

时间:2017-02-26 04:14:33

标签: java testing cookies mockito logout

嗨我有以下两种方法用于注销,另一种用于内部调用以清除cookie。我首先开始模拟删除cookie的第二种方法。我从DZone那里引用了这段代码。

我编辑了测试方法的代码。它运行正常但我无法获得Logout方法的解决方案

public class Service
{
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public String logout(HttpServletRequest request,HttpServletResponse response) {
        HttpSession session = request.getSession(true);
    session.setAttribute("sessionid","id");
    Cookie first=new Cookie("name","Mycookie");
    response.addCookie(first);
        if (request.isRequestedSessionIdValid() && session != null) {
        session.invalidate();
        }
        HttpServletResponse sessionRes=handle(request,response);
    return "You have successfully logged out";
        }


    public HttpServletResponse handle(HttpServletRequest request, HttpServletResponse response)
    {
        Cookie[] cookies = request.getCookies();
        if(cookies!=null || cookies.length!=0)
        {
            for(Cookie cookie:cookies)
            {
                cookie.setMaxAge(0);
                cookie.setValue(null);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
            response.addHeader("message", "No cookies found");
        }
        return response;
    }
}

处理注销工作正常如下,但无法测试注销方法,它会抛出空指针

@RunWith(MockitoJUnitRunner.class)
public class LogoutTest {
    @InjectMocks
    Service service;

    @Mock
    Cookie cookie;
    @Mock
    HttpServletRequest request;
    @Mock
    HttpServletResponse response;

    @Test
    public void TestHandle()
    {
        Cookie[] cookieArr = {cookie};
    response.addCookie(cookieArr[0]);
    when(request.getCookies()).thenReturn(cookieArr);
    service.handle(request, response);
   // verify(response).addCookie((Cookie) any());
    verify(response).addHeader("message", "No cookies found");
   Assert.assertEquals(cookie.getValue(),null);
    }

}

这是我的Logout测试方法,它引发了我奇怪的异常。我不知道我哪里出错了。我也调用了handle方法。

@RunWith(MockitoJUnitRunner.class)
public class LogoutTest {
    @InjectMocks
    Service service;

    @Mock
    Cookie cookie=new Cookie("fname","ram");
    @Mock
    HttpServletRequest request;
    @Mock
    HttpServletResponse response;
    @Mock
    HttpSession session;

    @Test
    public void TestLogout()
    {

    session=request.getSession();
    request.setAttribute("sid", "id");
    Cookie[] cookieArr = {cookie};
    response.addCookie(cookieArr[0]);
    when(request.getCookies()).thenReturn(cookieArr);

    when(service.handle(request, response)).thenReturn(response);
     Assert.assertEquals("You have successfully logged out",service.logout(request, response));
    }
}

1 个答案:

答案 0 :(得分:0)

你正在使用

做嘲弄错误
when(service.handle(request,response)).thenReturn(response);

表示service被模拟,并且从这种情况调用handle方法时,返回response,因此您根本不测试handle方法。< / p>

@Test
public void test_handle() {
    Cookie[] cookieArr = {new Cookie("fname","ram")};
    when(request.getCookies()).thenReturn(cookieArr);
    service.handle(request, response);
    verify(response).addCookie(any());
    verify(response).addHeader("message", "No cookies found");
}