tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <david.blev...@visi.com>
Subject Re: Injection into Unit Tests
Date Tue, 12 May 2009 06:50:33 GMT
Highly requested feature, no doubt.  Worked on it a couple weeks ago,  
but had to put it down to focus on other things for a bit.  Dug it  
back up today and got it to the point where it works for local clients  
using the LocalInitialContextFactory.  Would still like to get similar  
support in for remote clients using the RemoteInitialContextFactory,  
but that will have to wait for another time.

Uploaded new maven2 snapshots[1] and a new server zip[2] to our repo  
so that people can give it a try.  Also an example here[3].

It's all driven via new @LocalClient annotation used like so:

   @LocalClient
   public class MoviesTest extends TestCase {

       @EJB
       private Movies movies;

       @Resource
       private UserTransaction userTransaction;

       @PersistenceContext
       private EntityManager entityManager;

       public void setUp() throws Exception {
           Properties p = new Properties();
           p.put(Context.INITIAL_CONTEXT_FACTORY,  
"org.apache.openejb.client.LocalInitialContextFactory");
           p.put("movieDatabase", "new://Resource?type=DataSource");
           p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
           p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");

           InitialContext initialContext = new InitialContext(p);

           // Here's the fun part
           initialContext.bind("inject", this);
       }

       // ...
   }

Just add an empty META-INF/ejb-jar.xml or an empty META-INF/ 
application-client.xml to your test source and we will pickup your  
@LocalClient annotated classes and make them injectable via the  
LocalInitialContextFactory.

Give it a try and definitely let me know if you encounter any issues.


-David


[1] http://repository.apache.org/snapshots/
[2] http://repository.apache.org/content/repositories/snapshots/org/apache/openejb/openejb-standalone/3.1.1-SNAPSHOT/openejb-standalone-3.1.1-20090512.062318-3.zip
[3] http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/testcase-injection/

On May 10, 2009, at 7:29 AM, Reza Rahman wrote:
> Hi,
>
> I am currently using Spring JUnit 4 support classes to inject  
> OpenEJB Session Beans into unit tests. Here is the code:
>
> @RunWith(SpringJUnit4ClassRunner.class) // Bootstraps Spring inside  
> the test.
> @ContextConfiguration(locations = { "/applicationContext.xml" }) //  
> Responsible for instantiating OpenEJB and injecting the EJB below.
> public class AccountServiceTest {
>   @EJB(mappedName = "DefaultAccountServiceLocal") // Spring  
> processes this @EJB annotation to perform the injection from an  
> OpenEJB JNDI instance.
>   private AccountService accountService;
>
> I would like to avoid using Spring just for this. Does OpenEJB  
> provide support for anything similar to SpringJUnit4ClassRunner?
>
> Many thanks in advance,
> Reza
> ------------------------------------------------
> Expert Group Member, EJB 3.1 and Java EE 6
> Author, EJB 3 in Action
> Independent Consultant
>




Mime
View raw message