我的 JSF 应用程序的 wirint 测试用例有一些问题。所以我想测试我的注销方法:
FacesContext context = EasyMock.createMock(FacesContext.class);
String userName = "testUserName";
HttpSession session = EasyMock.createMock(HttpSession.class);
ExternalContext ext = EasyMock.createMock(ExternalContext.class);
EasyMock.expect(ext.getSession(true)).andReturn(session);
EasyMock.expect(context.getExternalContext()).andReturn(ext).times(2);
context.getExternalContext().invalidateSession();
EasyMock.expectLastCall().once();
EasyMock.replay(context);
EasyMock.replay(ext);
EasyMock.replay(session);
loginForm = new LoginForm();
loginForm.setUserName(userName);
String expected = "login";
String actual = loginForm.logout();
context.release();
Assert.assertEquals(expected, actual);
EasyMock.verify(context);
EasyMock.verify(ext);
EasyMock.verify(session);
我的注销方法是:
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/authentication/login.xhtml?faces-redirect=true";
}
我的问题是我在这里遇到了一个空指针异常: EasyMock.expectLastCall().once() 如何正确测试它?我想这与模拟有关,但我找不到解决方案,在这种情况下我怎么能正确模拟 FacesContext