3 Replies Latest reply on Dec 22, 2008 4:06 PM by nbhatia.bhatian.comcast.net

    How to test a Seam component?

    nbhatia.bhatian.comcast.net

      I have created a Seam component and would like to test it using TestNG. I read the relevant sections in the reference manual (sections 29.5 and 29.6) but unfortunately could not understand how to do this. Can someone please point me in the right direction?


      My component is called SecurityService with one method called createUser():


      @Name("securityService")
      public class SecurityService {
          @In private IdentityManager identityManager;
          @In(create=true) private PersonDao personDao;
      
          public void createUser(String username, String password, String role) {
              ...        
          }
      }
      



      My understanding is that I can extend SeamTest to test components. Here's my attempt:


      public class CreateUserTest extends SeamTest {
          
          @Test
          public void testCreateUser() throws Exception {
              new ComponentTest() {
                  @Override
                  protected void testComponents() throws Exception {
                      invokeMethod("#{securityService.createUser}");
                  }
              }.run();
          }
      }
      



      Anyway, I do not know how to invoke the method with the 3 parameters. Also do I need to set up JBoss Embedded? If so how (the reference manual shows how to do this for Tomcat, but not for TestNG)?


      Any help would be much appreciated?


      Thanks.


      Naresh

        • 1. Re: How to test a Seam component?
          nbhatia.bhatian.comcast.net

          Ok, I figured out that Integration Testing is documented in Chapter 34 of the reference manual - in the process of trying it out. However, a quick question - the example and the ComponentTest API suggest that we can only invoke methods with no parameters. Is it possible at all to test component methods that take parameters? If not, is the use of such methods discouraged? Why?


          Thanks.
          Naresh

          • 2. Re: How to test a Seam component?
            matt.nirgue

            Hi Naresh


            Actually you can test methods with parameters but you can't use invokeMethod to do so...


            You need to get your component first then call your method(s) like this:



            SecurityService securityService = (SecurityService) getInstance(SecurityService.class);
            securityService.createUser("username", "pwd", "role");



            • 3. Re: How to test a Seam component?
              nbhatia.bhatian.comcast.net

              Thanks so much Matt! I am now able to proceed with the test.


              I would suggest (to committers) to add this way of invoking a test in the docs (section 34.2) - the docs gave me the impression that invokeMethod is the only way. Also it would be nice to clarify the advantage of using invokeMethod vs. getInstance() followed by a direct call (which seems more clearer to me).


              Thanks again, Matt.