0

I have code on MainBacking.java like this :

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

I made FacesContextProvider class too and here is my 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;
    }
    
}

And here is my 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

When I run the test i got error null on MainBacking.java:11 Please help me. Thank You.

muhkanda
  • 319
  • 1
  • 9
  • Does this answer your question? [JUnit Testing FacesContext.getExternalContext](https://stackoverflow.com/questions/14207985/junit-testing-facescontext-getexternalcontext) – kasptom Nov 01 '21 at 10:26
  • Nope, When I try the answer I got an error : FacesContextProvider Not found. – muhkanda Nov 01 '21 at 10:32
  • Ok, as the author of the linked question wrote: "Where facesContextProvider is a custom class for returning the faces context (useful for mock testing)". Can you try to create your own `FacesContextProvider` and mock it like in the answer? – kasptom Nov 01 '21 at 10:34
  • 1
    Thankyou~ I'll try it =)) – muhkanda Nov 01 '21 at 10:38
  • @kasptom Please check update, Thank you! – muhkanda Nov 01 '21 at 10:51

2 Answers2

1

Looking at this answer and the article linked in it, I could not reproduce the NPE. That's how I've testesd it:

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();
    }
}
kasptom
  • 2,363
  • 2
  • 16
  • 20
0

Thank You for all response, I've solved my problem by reading 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 to set the mocked FacesContext as current instance in my test-case, the null error was solved. Thank You!

muhkanda
  • 319
  • 1
  • 9