5 Replies Latest reply on Feb 6, 2013 5:51 PM by Danny Althoff

    JSF session.

    mathewrock Newbie

      Hi.

      I´m using jsf session for my application and the problem are when I call any getInstance then return null.

       

      Any Idea?

       

      Declaration getInstance:

       

      public static LoginBB getInstance() {

              return FacesContext.getCurrentInstance().getApplication()

                      .evaluateExpressionGet(FacesContext.getCurrentInstance(),

                              "#{LoginBB}", LoginBB.class);

          }

       

      and when I called:

       

      LoginBB.getInstance().getUser();

        • 1. Re: JSF session.
          Aslak Knutsen Master

          What does your testcase look like?

          • 2. Re: JSF session.
            mathewrock Newbie

            This is my testcase:

             

            @RunWith(Arquillian.class)

            public class InformeRecMedicoTripulanteBBArquillianTest {

                @Inject

                private InformeRecMedicoTripulanteBB BackBean = new InformeRecMedicoTripulanteBB();

             

                @Deployment

                public static JavaArchive createDeployment() {

                    return ShrinkWrap.create(JavaArchive.class, InformeEntrenamientosTripulanteBB.class.getName() + "test.jar")

                            .addClasses(

                                    //backingbean.

                                    InformeRecMedicoTripulanteBB.class, ControllerReportBase.class,

                                    ControllerReportListadoReconocimientoMedico.class, CuadroResumenBB.class,

                                    ControllerLogIn.class, UtilidadesTiempo.class,

                                    //clases padres.

                                    IBackingBean.class, EntidadSiucom.class,

                                    GenericBackingBean.class,

                                    //services.

                                    HistoricoTripulanteService.class, UsuarioServiceLocal.class,

                                    UnidadAplicacionService.class, CompetenciaLinguisticaService.class,

                                    RMedicoTripulanteService.class, HistCargoTripulanteService.class,

                                    //entidades.

                                    HistoricoTripulante.class, HistoricoTripulantePK.class,

                                    EstadoTripulante.class, HorasInicializadasTripulante.class,

                                    HorasInicializadasTripulantePK.class, HistCargoTripulante.class,

                                    HistCargoTripulantePK.class, CargoTripulante.class,

                                    UnidadAerea.class, SistemaArmasParticular.class,

                                    Aeronave.class, Usuario.class,

                                    TripulanteEntrenamiento.class, UnidadAplicacion.class,

                                    HorasFuncion.class, CompetenciaLinguistica.class,

                                    ReconocimientoMedicoTripulante.class, ReconocimientoMedicoTripulantePK.class,

                                    TripulanteReconocimientoMedico.class, UnidadAerea.class,

                                    TipoFuerzas.class, AdaptadorEstacionCarga.class,

                                    TipoFAC.class, Empleo.class,

                                    SistemaArmasInspeccion.class, Diario.class,

                                    DiarioPK.class, NivelEstado.class,

                                    NivelEstadoPK.class, AeronaveBaja.class,

                                    AeronaveBajaPK.class, ConfiguracionAeronave.class,

                                    ConfiguracionAeronavePK.class, ElementoConfiguracion.class,

                                    ElementoConfiguracionPK.class, Pylon.class,

                                    ArmaCargaEstacion.class, HistoricoConfiguracionAeronave.class,

                                    HistoricoConfiguracionAeronavePK.class, GrupoConfiguracion.class,

                                    BaseAerea.class, Localidad.class,

                                    SistemaArmasGenerico.class, SistemaArmasInspeccionPK.class,

                                    CalificacionOperativa.class, Indicativo.class,

                                    TipoInspeccion.class, SitAdministrativa.class,

                                    AjenoEA.class, TipoAeronave.class,

                                    MotivoPerdidaCalificacion.class, EstadoMantenimiento.class,

                                    SistemaArmasParticular.class, MandoGenerador.class,

                                    Mac.class, SituacionAeronave.class,

                                    Pais.class, GrupoSanguineo.class,

                                    NivelMantenimiento.class, Aeropuerto.class,

                                    TipoAvion.class, Tripulante.class,

                                    ModoAcceso.class, TipoReconocimientoMedico.class,

                                    AptitudReconocimientoMedico.class, AeronaveInspeccion.class,

                                    Escuadron.class, HorasOperacionalesTripulante.class,

                                    HorasOperacionalesTripulantePK.class, HistoricoHorasOperacionalesTripulante.class,

                                    HistoricoHorasOperacionalesTripulantePK.class, HorasVueloTripulante.class,

                                    HorasVueloTripulantePK.class,

                                    //daos.

                                    HistCargoTripulanteDaoLocal.class, EscuadronDaoLocal.class,

                                    TipoEntrenamientoDaoLocal.class, UnidadAereaDaoLocal.class,

                                    SistemaArmasParticularDaoLocal.class, TipoReconocimientoMedicoDaoLocal.class,

                                    CargoTripulanteDaoLocal.class, HorasHHVDaoLocal.class,

                                    HorasHTRDaoLocal.class, HorasHOCNavDaoLocal.class,

                                    HorasOCHNavDaoLocal.class, HorasHOCNctDaoLocal.class,

                                    HorasOCHNctDaoLocal.class, HorasHOCInsDaoLocal.class,

                                    HorasOCHInsDaoLocal.class, HorasHOCCmdDaoLocal.class,

                                    HorasOCHCmdDaoLocal.class, HistoricoTripulanteDaoLocal.class,

                                    CompetenciaLinguisticaDaoLocal.class, UnidadAplicacionDaoLocal.class,

                                    RecMedicoTripulanteDaoLocal.class, GenericoDAOLocal.class,

                                    UsuarioDaoLocal.class,

                                    //Dao

                                    GenericoDAO.class, HistCargoTripulanteDao.class,

                                    UsuarioDao.class, HorasHOCInsDao.class,

                                    CompetenciaLinguisticaDao.class, TipoReconocimientoMedicoDao.class,

                                    HistoricoTripulanteDao.class, HorasHOCCmdDao.class,

                                    HorasOCHNavDao.class, EscuadronDao.class,

                                    HorasOCHNctDao.class, HorasHHVDao.class,

                                    RecMedicoTripulanteDao.class, UsuarioService.class,

                                    HorasHOCNavDao.class, UnidadAplicacionDao.class,

                                    UnidadAereaDao.class, HorasOCHNctDao.class,

                                    HorasOCHInsDao.class, HorasHOCNctDao.class,

                                    CargoTripulanteDao.class, HorasOCHCmdDao.class,

                                    HorasHTRDao.class,

                                    //Primefaces

                                    StreamedContent.class,

                                    //jasperreport

                                    net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.class, net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.class,

                                    net.sf.jasperreports.engine.JRRewindableDataSource.class, net.sf.jasperreports.engine.JRDataSource.class,

                                    net.sf.jasperreports.engine.JasperPrint.class, net.sf.jasperreports.engine.JRPropertiesHolder.class,

                                    net.sf.jasperreports.engine.JRException.class, net.sf.jasperreports.engine.design.JasperDesign.class,

                                    net.sf.jasperreports.engine.design.events.JRChangeEventsSupport.class, net.sf.jasperreports.engine.base.JRBaseReport.class,

                                    net.sf.jasperreports.engine.JRReport.class, net.sf.jasperreports.engine.JRDefaultFontProvider.class,

                                    net.sf.jasperreports.engine.JRDefaultStyleProvider.class, net.sf.jasperreports.engine.JasperReport.class,

                                    net.sf.jasperreports.engine.util.JRVisitorSupport.class, net.sf.jasperreports.engine.JRVisitor.class,

                                    //bbdd

                                    ea.ciges.siucom.zsv.ejb.exceptions.dispatchers.ErrorDispatcher.class, ea.ciges.siucom.zsv.ejb.exceptions.factory.BDFactory.class

                            )

                            .addAsResource("test-persistence.xml", "META-INF/persistence.xml")

                            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

                }

                @Test

                public void init() {

                    //mando

                    BackBean.loadData();

                    //no mando

                    BackBean.loadData();

                    BackBean.setFiltroSQN("131");

                    BackBean.loadData();

                }

                @Test

                public void shouldCargaListaSqnUnidadAereaAJAX() {

                    DummyAjaxBehaviorEvent dAjax = new DummyAjaxBehaviorEvent(null, null);

                    dAjax.getComponent().getAttributes().put("value","3");

                    BackBean.setFiltroUnidad("12");

                    BackBean.cargaListaSqnUnidadAerea(dAjax);

                }

                @Test

                public void shouldGetFile() {

                    BackBean.getFile();

                }

            }

             

            When I call BackBean.loadData() it ask to login who are user but I don´t entry in app my facesContext are null. Before when I test with mock I just  mock return but I dont know how inicializate facesContext here.

            • 3. Re: JSF session.
              mathewrock Newbie

              any idea?????

              • 4. Re: JSF session.
                Aslak Knutsen Master

                You're not doing a JSFRequest, so no FacesContext is active at the time the test run.

                 

                If you want JSF active you'd need to use @RunAsClient on the TestMethod and invoke the JSFPage via HTTP.

                 

                In your case, you want to control the data and invoke jsf. Arquillian Warp is probably more suited for this need.

                 

                Example test case can be seen here:

                https://github.com/arquillian/arquillian-extension-warp/blob/master/extension/jsf-ftest/src/test/java/org/jboss/arquillian/warp/jsf/ftest/BasicJsfTest.java

                • 5. Re: JSF session.
                  Danny Althoff Newbie

                  You can @Inject the FacesContext, but i am currently having problems to test this either, but maybe it will help you

                   

                  package de.somePackage;

                   

                  import javax.enterprise.context.RequestScoped;

                  import javax.enterprise.inject.Produces;

                  import javax.faces.context.FacesContext;

                   

                  /**

                  * Makes it possible to inject a FacesContext. Makes it possible to mock that

                  * for testing.

                  *

                  * source 1: http://web.archiveorange.com/archive/v/fhwU2FmwjbzqE6H9m0jT <br />

                  * source 2: http://blog.m1key.me/2010/11/jsf-inject-facescontext.html

                  *

                  * @author Danny Althoff

                  */

                  public class FacesContextProducer {

                   

                      @Produces

                      @RequestScoped

                      public FacesContext getFacesContext() {

                          return FacesContext.getCurrentInstance();

                      }

                  }

                   

                  To mock it in testing i use this here (but as stated above, its not working ... but im on TomEE, maybe i am just doing something REALLY wrong) :

                   

                  package de.somePackage.mocked;

                   

                  import java.io.Serializable;

                  import javax.enterprise.context.ApplicationScoped;

                  import javax.enterprise.inject.Produces;

                  import javax.faces.component.UIViewRoot;

                  import javax.faces.context.FacesContext;

                  import static org.mockito.Mockito.*;

                   

                  /**

                  * Makes it possible to inject a FacesContext for Testing.

                  *

                  * source 1: http://web.archiveorange.com/archive/v/fhwU2FmwjbzqE6H9m0jT <br />

                  * source 2: http://blog.m1key.me/2010/11/jsf-inject-facescontext.html

                  *

                  * @author Danny Althoff

                  */

                  public class MockedFacesContextProducer implements Serializable {

                   

                      public static final long serialVersionUID = 1L;

                   

                      @Produces

                      @ApplicationScoped // has to be THIS scope, other scoped are NOT working here !!!

                      public FacesContext getMockedContext() {

                          FacesContext mockedFC = mock(FacesContext.class);

                   

                          UIViewRoot mockedViewRoot = mock(UIViewRoot.class);

                          when(mockedViewRoot.getViewId()).thenReturn("/index.xhtml");

                   

                          when(mockedFC.getViewRoot()).thenReturn(mockedViewRoot);

                          return mockedFC;

                      }

                  }