Search code examples
java-ee-6jboss-arquilliancodi

How do i use ViewAccsessScope beans when testing in Arquillian?


When i use @ViewAccsessScoped beans from CODI i get the following error, when using Arquillian for testing my beans.

org.jboss.arquillian.test.spi.ArquillianProxyException: org.jboss.weld.context.ContextNotActiveException : WELD-001303 No active contexts for scope type org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped

is there anyway to get this to work?

//Trind


Solution

  • I tried to mock the FacesContext and got it working no idea if this is the best approch however. I used Mockito to when doing the mocks.

        FacesContext mock = null;
    
        final Map<Object, Object> attributes = new HashMap<Object, Object>();
    
        public void mockFacesContext() {
    
            if (mock == null) {
                mock = Mockito.mock(FacesContext.class);
    
                try {
                    Method m = FacesContext.class.getDeclaredMethod(
                            "setCurrentInstance", FacesContext.class);
                    m.setAccessible(true);
                    m.invoke(FacesContext.class, mock);
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                Mockito.when(mock.getAttributes()).thenReturn(attributes);
    
                ExternalContext ext = Mockito.mock(ExternalContext.class);
                Mockito.when(ext.getSession(false)).thenReturn(
                        Mockito.mock(HttpSession.class));
    
                Mockito.when(mock.getExternalContext()).thenReturn(ext);
    
                UIViewRoot uiViewRoot = Mockito.mock(UIViewRoot.class);
                Mockito.when(uiViewRoot.getViewId()).thenReturn("/test");
                Mockito.when(uiViewRoot.getLocale()).thenReturn(new Locale("se"));
                Mockito.when(mock.getViewRoot()).thenReturn(uiViewRoot);
    
                Application application = Mockito.mock(Application.class);
                Mockito.when(application.getSupportedLocales()).thenReturn(
                        Mockito.mock(Iterator.class));
    
                Mockito.when(mock.getApplication()).thenReturn(application);
    
            }
            return mock;
        }