2 Replies Latest reply on Jan 17, 2008 5:47 AM by Olivier Thierry

    How to pass parameters with ComponentTest.invokeMethod

    Olivier Thierry Apprentice

      Hi,

      I write integration tests with EJB3 embedded container and I need to test a method needing parameters. I did it this way :

      @org.testng.annotations.Test
      public void testEmploye() throws Exception {
       new ComponentTest() {
       protected void testComponents() throws Exception
       {
       Employe employe = new Employe();
       employe.setMatricule("000001");
       employe.setNom("nom");
       employe.setPrenom("prenom");
       employe = (Employe) invokeMethod("#{coreServiceEmploye.saveEmploye(employe)}");
      
       assert employe.getId() != null;
       }
       }.run();
      }


      But the "employe" parameter is not passed to coreServiceEmploye.saveEmploye method (it has a null value). I found only examples with setValue() to pass datas to components, but I could not find anything about how to pass parameters in a method on forums or anywhere. How should I do this ?

      Thanks in advance

      Olivier

        • 1. Re: How to pass parameters with ComponentTest.invokeMethod
          Olivier Thierry Apprentice

          Well I read that you can't use parameters with invokeMethod. So I tried with Expressions.createMethodExpression :

          MethodExpression<Employe> me = Expressions.instance().createMethodExpression(
           "#{coreServiceEmploye.saveEmploye(employe)}",
           Employe.class,
           new Class[]{Employe.class});
          employe = me.invoke(employe);


          But still the same problem, employe parameter is null in coreServiceEmploye.saveEmploye method. I really can't understand why. Anyone has an idea ?

          • 2. Re: How to pass parameters with ComponentTest.invokeMethod
            Olivier Thierry Apprentice

            I gave up using reflection to call Seam component and used getInstance() method instead to get the Seam component and then call its methods as usual. So now it looks like this :

            @org.testng.annotations.Test (
            public void testEmploye() throws Exception {
             new ComponentTest() {
             @Override
             protected void testComponents() throws Exception {
             CoreServiceEmployeLocal serviceEmploye =
             (CoreServiceEmployeLocal) getInstance("coreServiceEmploye");
            
             Employe employe = new Employe();
             employe.setMatricule("000001");
             employe.setNom("nom");
             employe.setPrenom("prenom");
             employe = serviceEmploye.saveEmploye(employe);
            
             assert employe.getId() != null;
             }
             }.run();
            }


            It looks more simple to me, but are there things that will not work testing this way ? What is the interest of using invokeMethod() then ?