2013-10-16 147 views
0

我的JSF应用程序的wirint测试用例存在一些问题。所以我想测试我的注销方法:JSF - JUnit FacesContext模拟测试

  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()一次() 应该如何进行适当的测试?我想这是一些与嘲笑,但我不能找到一个解决方案,我怎么能正确地嘲笑的FacesContext在这种情况下

+0

为什么要测试'logout()'?它没有逻辑,它只使用'javax.faces'中的方法(你应该假设它已经被开发团队正确地测试过了)。 –

回答

2

为了让你可以使用例如PowerMock这是一个框架,允许扩展模拟库,例如EasyMock的与额外的功能上述工作。在这种情况下,它允许您模拟FacesContext的静态方法。

如果您正在使用Maven,使用以下link检查所需要的相关性设置。

使用这两个批注注释您的JUnit测试类。第一个注释告诉JUnit使用PowerMockRunner运行测试。第二个注释告诉PowerMock准备模拟FacesContext类。

@RunWith(PowerMockRunner.class) 
@PrepareForTest({ FacesContext.class }) 
public class LoginFormTest { 

现在继续使用模拟FacesContextPowerMock就像你对其他类的功能。唯一不同的是,这次你执行replay()verify()而不是实例。

@Test 
public void testLogout() { 

    // mock all static methods of FacesContext 
    PowerMock.mockStatic(FacesContext.class); 

    FacesContext context = EasyMock.createMock(FacesContext.class); 
    ExternalContext ext = EasyMock.createMock(ExternalContext.class); 

    EasyMock.expect(FacesContext.getCurrentInstance()).andReturn(context); 
    EasyMock.expect(context.getExternalContext()).andReturn(ext); 

    ext.invalidateSession(); 
    // expect the call to the invalidateSession() method 
    EasyMock.expectLastCall(); 
    context.release(); 

    // replay the class (not the instance) 
    PowerMock.replay(FacesContext.class); 
    EasyMock.replay(context); 
    EasyMock.replay(ext); 

    String userName = "testUserName"; 
    LoginForm loginForm = new LoginForm(); 
    loginForm.setUserName(userName); 

    String expected = "/authentication/login.xhtml?faces-redirect=true"; 
    String actual = loginForm.logout(); 
    context.release(); 

    Assert.assertEquals(expected, actual); 

    // verify the class (not the instance) 
    PowerMock.verify(FacesContext.class); 
    EasyMock.verify(context); 
    EasyMock.verify(ext); 
} 

我创建了一个blog post,它更详细地解释了上述代码示例。

0

此:

FacesContext context = EasyMock.createMock(FacesContext.class); 

而且不能改变这个返回值(在类正在测试中):

FacesContext.getCurrentInstance() 

您需要延长FacesContext然后调用保护方法setCurrentInstance你嘲笑的FacesContext。

相关问题