What I usually do is to avoid calling static methods in beans, which I want to test . This means that your current code needs to be reorganized:
FacesContext.getCurrentInstance().getExternalContext() .getSessionMap().get("paramKey");
Are there any ways to test them with my static method calls? Probably there is, but they led me to more trouble than help. Therefore, in the end I got rid of them and changed my design. Just let the second bean do it (which you will mock later). In your case, create an @SessionScoped bean that controls this functionality:
@ManagedBean @SessionScoped public class SessionBean{ public Object getSessionParam(String paramKey){ FacesContext.getCurrentInstance().getExternalContext() .getSessionMap().get(paramKey); } }
And add this bean to each individual bean that it needs (I usually extend my view / request beans from the abstract bean that has it, so I don't need to implement it in every bean):
@ManagedBean @RequestScoped public class RequestBean{ @ManagedProperty(value="#{sessionBean}") private SessionBean sessionBean; public void accessSessionParam(){ sessionBean.getSessionParam("name"); } }
This way, you can easily access static methods using the helper SessionBean . Then how to check it? Just create a layout (using Mockito , for example):
public class Test{ public void test1(){ SessionBean sb = Mockito.mock(SessionBean.class);
source share