5 Replies Latest reply on Dec 1, 2008 10:14 AM by Matt Nirgue

    injection (@In) in test class?

    Matt Nirgue Newbie

      Hi everyone...


      I've written a lot of integration tests in the last few weeks and I just wanted to know if it's possible to use injection within test classes to have something like:



      public class NotMuchTest extends SeamTest {
          @In
          EntityManager entityManager;
      
          @Test
          public void testNothing() throws Exception {
              new ComponentTest() {
                  @Override
                  protected void testComponents() throws Exception {
                      assertFalse(isEntityManagerNull());
                      Query query = entityManager.createQuery("select p from Person p");
                      assert query.getResultList().size() > 0;
                  }
              }.run();
          }
      
          private boolean isEntityManagerNull() {
              return entityManager == null ? true : false;
          }
      
      }



      Obviously this test doesn't do much but this is just to give you an example of what I'd like to do...
      I know I can get my entityManager using getInstance or getValue but it'd be great if I could directly use injection into my tests... Call me lazy or whatever but I'd really like to be able use my entityManager in my private methods without having to initialize it in each test methods.
      Do you know what I mean?


      I tried different things but I always got a NPE so I gave up trying :/ I'm just wondering if it's possible at all or if I'm just the one who's doing something wrong...


      Thanks in advance guys!


      Matt

        • 1. Re: injection (@In) in test class?
          Pedro Sena Newbie

          Hi,


          You got your NullPointerException because your test class is not seam-managed, to achieve this, it should have a @Name annotation (what is not necessary for a test class...)


          Instead of doing this, to receive any Seam component inside your class file you could do:



           protected void testComponents() throws Exception {
                          entityManager = (EntityManager) getValue("entityManager");
                          assertFalse(isEntityManagerNull());
                          Query query = entityManager.createQuery("select p from Person p");
                          assert query.getResultList().size() > 0;
                      }





          Use this code inside your ComponentTest, because is this class the provides this very useful method.


          Hope this helps,


          Pedro Sena

          • 2. Re: injection (@In) in test class?
            Matt Nirgue Newbie

            Hi (and thanks) Pedro


            I know that my test class is not seam-managed and that I can get my entityManager using getValue or getInstance (this is actually what I've used in my integration tests so far)... but I hoped there'd be a way to get my entityManager for the whole class methods without initializing it in every test methods but well, I guess there's not... Nevermind...


            Btw, could anyone explain to me the difference between getValue and getInstance?


            When I test my component I tend to use getInstance to get my components (or my entityManager)


            MyComponent mc = (MyComponent) getInstance("myComponent");


            but I know that


            MyComponent mc = (MyComponent) getValue("#{myComponent}");


            also works... and from what I saw, both return the same instance of my component!


            so what I'd like to know is: what's the right way to get my components, entityManager, ... : getInstance or getValue? Cause I don't really get the difference between these two solutions...


            Thanks again for helping me out

            • 3. Re: injection (@In) in test class?
              Ilya Newbie

              but I hoped there'd be a way to get my entityManager for the whole class methods without initializing it in every test methods but well, I guess there's not... Nevermind...

              Well, at least you can try junit solution like using test-suites or something ...

              • 4. Re: injection (@In) in test class?
                Matt Drees Master

                Matt Nirgue wrote on Nov 28, 2008 10:03:


                could anyone explain to me the difference between getValue and getInstance?



                Hi Matt,
                getValue() will evaluate an EL expression.  So, you could call getValue(#{myComponent.name}) if you wanted (assuming MyComponent has a name property).  So, because of Seam's ELResolver, you can look up components, but you can do other things as well.


                Component.getInstance() simply does Seam lookups (components, @Factory values, @Unwrap values).  I would recommend this approach over the previous option, though instead of the string version, I would use


                MyComponent mc = (MyComponent) Component.getInstance(MyComponent.class);
                



                which, though still not typesafe, is easier to refactor.

                • 5. Re: injection (@In) in test class?
                  Matt Nirgue Newbie

                  Thanks a lot Matt ;)


                  As I said, I've mostly used getInstance in my tests because it seemed to be the logic choice but I wasn't sure...


                  Thank you for helping me with this...