-
1. Re: injection (@In) in test class?
pedrosena Nov 28, 2008 12:18 AM (in response to matt.nirgue)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 Nov 28, 2008 10:03 AM (in response to matt.nirgue)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?
atamur Nov 28, 2008 6:50 PM (in response to matt.nirgue)
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 Nov 29, 2008 6:37 AM (in response to matt.nirgue)
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 aname
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 Dec 1, 2008 10:14 AM (in response to matt.nirgue)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...