aspose file tools*
The moose likes Testing and the fly likes Dependency in the container due to context.lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Dependency in the container due to context.lookup" Watch "Dependency in the container due to context.lookup" New topic
Author

Dependency in the container due to context.lookup

avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
Hey,

Ejb3 has a big step a head with the POJO idea.
its help a lot for decrease the dependency with the container.

i would like to consult with you how to write a test for a class that depend in statless (ejb3) and obtain it by using context.lookup

Thank you
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You mean you have a class that does something like this?


If that's the case, all you need to do is grab the MockContextFactory class from the MockEJB project and fake the JNDI tree. There's an example of this in the freely available online bonus chapter of my book.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
Thank you.

I am actually in the middle of the book and i might need to start from the bonus chapter (i am working with JPA and Ejb3 with JBoss).

Do you have there a solution for injection.

I mean when you have in your ejb a resource injection like @Ejb and etc
I dont want to insert a IOVC framework for testing only.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by avihai marchiano:
I mean when you have in your ejb a resource injection like @Ejb and etc
I dont want to insert a IOVC framework for testing only.

Well, you don't need any frameworks/libraries if you make your @EJB/@Resource annotated fields visible (protected, package private, public) to the test code. If you don't want to do that, you can use Java's reflection API to forcibly assign a mock to the annotated field. That's a bit clunky, though, so it would probably be easier to use a library that does the nasty reflection stuff for you. If this is the way to go, I'd recommend BeanInject.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependency in the container due to context.lookup
 
Similar Threads
Where should the connection be obtained in the EJB
Using generics to avoid casting?
ClassCast exception working with Remote session bean
Cannot cast org.hibernate.validator.util.LazyValidatorFactory to javax.validation.ValidatorFactory
Advantages of EJB 3/JPA over spring/hibernate