Mocking FacesContext

前端 未结 7 1108
执念已碎
执念已碎 2021-02-18 19:20

I am trying to add some unit tests to a JSF application. This application didnt rely heavily on any best practices, so many service methods use the FacesContext to

7条回答
  •  -上瘾入骨i
    2021-02-18 19:45

    I believe the best solution is not presented here. Here we go

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({ FacesContext.class})
    public class MyTestClass{
    
    @Mock
    private FacesContext facesContext;
    
    @Before
    public void init() throws Exception {
            PowerMockito.mockStatic(FacesContext.class);
            PowerMockito.when(FacesContext.getCurrentInstance()).thenReturn(facesContext);
    }
    

    And you need to import all the PowerMockito bundle in your pom.xml

            
                org.mockito
                mockito-all
                ${mockito.version}
                test
            
            
                org.powermock
                powermock-api-mockito
                ${powermock.version}
                test
            
            
                org.powermock
                powermock-module-junit4
                ${powermock.version}
                test
            
    

提交回复
热议问题