4 Replies Latest reply on Apr 16, 2009 6:26 AM by I need a real name

    No injection into seam test

    Michel Jazz Newbie
      I have created a Seam project with jboss tools (Eclipse). In this project, I have created an entity

      @Entity
      @Name("user")
      @Scope(SESSION)
      public class User implements Serializable {
              private Integer userId;
              private String username;
              private String passwordHash;
              private String firstname;
              private String lastname;
              private boolean enabled;
              private boolean admin;
              //Getters and setters
      }

      and a SFB, which manages this entity

      @Local
      public interface UserAction {
        public User getUser(String username, String password);
        public void create(User user);
        public void create();
        public void destroy();
      }

      @Stateful
      @Name("userAction")
      public class UserActionBean extends EntityHome<User> implements UserAction {
              @In StatusMessages statusMessages;
              @Logger private Log log;
              @In EntityManager entityManager;
              public void create(User user) {
                      entityManager.persist(user);
                      statusMessages.add("User " + user.getUsername() + " created!");
                      return user;
              }
             
              //Etc …
              @Override
              public void create() {
                     super.create();
              }

              @Remove
              @Destroy
              public void destroy() {}
              }

      I just want to test my session. So :

      public class UserActionTest extends SeamTest {
              @In     UserAction userAction;

              @Test
              public void testUserActionCreate() throws Exception {
                      User user = new User();
                      user.setAdmin(true);
                      user.setEnabled(true);
                      user.setFirstname("Oncle");
                      user.setLastname("Zebulon");
                      user.setPasswordHash("password");
                      user.setUsername("onclezeb");
                      userAction.create(user);
                      System.out.println(user.getUserId());
              }
      }

      The problem is that userAction is never injected. The embedded container start  successfuly, i see the thread UserActionBean but the userAction is null.

      Any ideas ?
      Thx
        • 1. Re: No injection into seam test
          Matt Nirgue Newbie

          instead of using injection in your test, maybe you should try something like:


          UserAction userAction = (UserAction)getValue("#{userAction}");



          I haven't tried to use injection in my tests so far so I don't know if it can be done or not but I do know that my tests work just fine using getValue(...) and/or getInstance(...)


          Hope it helps you...



          • 2. Re: No injection into seam test
            Michel Jazz Newbie
            Hi Matt,

            You are right :
            public class UserActionTest extends SeamTest {
                 @Test
                 public void testUserActionCreate() throws Exception {
                      new FacesRequest() {
                           @Override
                           protected void invokeApplication() {
                                UserAction userAction = (UserAction)getValue("#{userAction}");
                                User user = new User();
                                user.setAdmin(true);
                                user.setEnabled(true);
                                user.setFirstname("Oncle");
                                user.setLastname("Zebulon");
                                user.setPasswordHash("password");
                                user.setUsername("onclezeb");
                                userAction.create(user);
                                System.out.println(user.getUserId());
                           }
                      }.run();
                 }
            }

            It's ok
            Thx.
            • 3. Re: No injection into seam test
              I need a real name Newbie
              my components is ejb components

              @Name("paymentOrderDao")
              @AutoCreate
              @Stateless
              @SuppressWarnings("unchecked")
              public class PaymentOrderDaoImpl implements PaymentOrderDao{
                 
                 public void findById(){

                 }

              }


              test code

              public class PaymentOrderDaoTest extends SeamTest{

                      @Test
                      public void testFindOrderByOrderId() throws Exception{
                               new FacesRequest() {
                                 @Override
                                 protected void invokeApplication() {
                                         PaymentOrderDao paymentOrderDao = (PaymentOrderDao) getValue("#{paymentOrderDao}");
                                              assert paymentOrderDao != null;
                                 }
                               }.run();
                             
                      }

              }


              runtime getValue("#{paymentOrderDao}")
              error:
              Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: paymentOrderDao

              why???
              • 4. Re: No injection into seam test
                I need a real name Newbie
                <blockquote>
                _I need a real name wrote on Apr 15, 2009 09:25:_<br/>

                my components is ejb components

                @Name("paymentOrderDao")
                @AutoCreate
                @Stateless
                @SuppressWarnings("unchecked")
                public class PaymentOrderDaoImpl implements PaymentOrderDao{
                   
                   public void findById(){

                   }

                }


                test code

                public class PaymentOrderDaoTest extends SeamTest{

                        @Test
                        public void testFindOrderByOrderId() throws Exception{
                                 new FacesRequest() {
                                   @Override
                                   protected void invokeApplication() {
                                           PaymentOrderDao paymentOrderDao = (PaymentOrderDao) getValue("#{paymentOrderDao}");
                                                assert paymentOrderDao != null;
                                   }
                                 }.run();
                               
                        }

                }


                runtime getValue("#{paymentOrderDao}")
                error:
                Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: paymentOrderDao

                why???
                </blockquote>


                this code in seam2.0 version is success but in seam2.1 version is fail  why????