2013-10-16 143 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。

相關問題