jsf test中的Mock FacesContext会导致NullpointerException

时间:2016-05-03 19:53:29

标签: jsf powermock

我想测试我的ManagedBean的操作:

@ManagedBean(name = "aMB")
@SessionScoped
public class AManagedBean {
    static FacesContext fc = FacesContext.getCurrentInstance();
    static ServletContext sc = (ServletContext)   fc.getExternalContext().getContext();

}

我的测试班:

@RunWith(PowerMockRunner.class)

@PrepareForTest({ FacesContext.class })

   public class ATest {
     private AManagedBean aMB;
     private FacesContext faceContext;
     private ExternalContext externalContext;
    @Before
    public void setup() throws Exception {
      faceContext = Mockito.mock(FacesContext.class);
      externalContext = Mockito.mock(ExternalContext.class);         Mockito.when(this.faceContext.getExternalContext()).thenReturn(externalContext);
     aMB = new AManagedBean(); // In this part, it throws NullPointerException because of FacesContext call.
}

我不是jsf大师,我不知道我做错了什么

0 个答案:

没有答案