2 Replies Latest reply on Mar 5, 2010 10:34 PM by trappa

    Creating an EL expression in a Seam unit test

    trappa

      I need to write a unit test that creates a list of FilterField.



      private List<FilterField> createSampleFilters() {
        List<FilterField> filters = new ArrayList<FilterField>();
        ExtendedFilterField filtre = 
          new ExtendedFilterField(
            getExpression("#{user.username}"),"j"); 
        
        filters.add(filtre);
        return filters;
      }
      
      private Expression getExpression(String exp) {
        ELContext context = 
                    FacesContext.getCurrentInstance().getELContext();
      
        return ExpressionFactory.newInstance().
                 createValueExpression(context, exp, String.class);
      }
      




      MY PROBLEM is that FacesContext.getCurrentInstance() throws a NullPointerException !


      I tried to create a FacesContext instance with MockFacesContextFactory but it doesn't work either :-/


      Does anyone have a clue to create a FilterField or a javax.el.Expression or a non null facesContext in a seam unit test ???

        • 1. Re: Creating an EL expression in a Seam unit test
          germanescobar

          If you are using ComponentTest, FacesRequest or NonFacesRequest for your test, you can call the getValue() method to evaluate an EL expression. For example:




          ...
          new ComponentTest() {
            @Override
            protected void testComponents() throws Exception {
              User user =  (User) getValue("#{user}");
              ...
            }
          }.run();
          ...






          If you don't know what I'm talking about, I'd suggest you take a look at the documentation first.

          • 2. Re: Creating an EL expression in a Seam unit test
            trappa

            Sorry, but I am not trying to evaluate an EL expression! I want to create a javax.el.Expression object in a seam unit test (and not an integration test)


            Actually, I am trying to create an ExtendedFilterField with this constructor



            ExtendedFilterField(javax.el.Expression expression, java.lang.String filterValue) 




            can anyone help ???