如何模拟来自 Faces Context 外部上下文的请求?

How to Mocking Request from Faces Context External Context?

我在 MainBacking.java 上有这样的代码:

public void init() {
    // Blah... 
    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); // line 10
    HttpServletRequest request = (HttpServletRequest) context.getRequest(); // line 11
    // Blah...
}

我也制作了 FacesContextProvider class,这是我的 FacesContextProvider.java

public abstract class FacesContextProvider extends FacesContext {

    private FacesContextProvider() {
    }

    private static final Release RELEASE = new Release();

    private static class Release implements Answer<Void> {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            setCurrentInstance(null);
            return null;
        }
    }

    public static FacesContext getCurrentInstance() {
        FacesContext context = Mockito.mock(FacesContext.class);
        setCurrentInstance(context);
        Mockito.doAnswer(RELEASE).when(context).release();
        return context;
    }
    
}

这是我的 test.java

FacesContext mockFacesContext = FacesContextProvider.getCurrentInstance();
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
ExternalContext mockExternalContext = Mockito.mock(ExternalContext.class);

when(mockFacesContext.getExternalContext()).thenReturn(mockExternalContext);
when(mockExternalContext.getRequest()).thenReturn(mockRequest);

// Run Test
mainBackingUnderTest.init(); // has been declare at top using @InjectMocks

当我 运行 测试时,我在 MainBacking.java:11 上得到了错误 null 请帮助我。谢谢。

看着里面的this answer and the article linked,我无法重现NPE。 这就是我测试它的方式:

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.*;

public class MainBacking {
    public void init() {
        // Blah...
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); // line 10
        HttpServletRequest request = (HttpServletRequest) context.getRequest(); // line 11
        // Blah...
    }
}
class MainBackingTest {

    MainBacking mainBackingUnderTest = new MainBacking();

    @Test
    void testInit() {
        // given
        FacesContext mockFacesContext = FacesContextProvider.getCurrentInstance();
        HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
        ExternalContext mockExternalContext = Mockito.mock(ExternalContext.class);

        when(mockFacesContext.getExternalContext()).thenReturn(mockExternalContext);
        when(mockExternalContext.getRequest()).thenReturn(mockRequest);

        // when
        try {
            mainBackingUnderTest.init();
        } finally {
            mockFacesContext.release();
        }

        // then
        verify(mockFacesContext, times(1)).getExternalContext();
        verify(mockExternalContext, times(1)).getRequest();
    }
}

感谢您的所有回复,我已经通过阅读 this article. I didn't know whats wrong when I'm using FacesContextProvider I'm still got null error, but when I'm using Omnifaces 将模拟的 FacesContext 设置为我的测试用例中的当前实例解决了我的问题,空错误已解决。谢谢!