Neat stuff.

Incidentally, OpenEJB will handle the injection for you:
http://openejb.apache.org/3.0/local-client-injection.html

Cheers,
Laird

On Wed, Sep 23, 2009 at 8:29 AM, Quintin Beukes <[email protected]> wrote:

> Hey,
>
> I know this was mentioned the other day, though I can't find the
> e-mail, so I'm making a new thread. I attached a simple runner which I
> made as a personal PoC. It basically allows you to define a test as
> follows:
>
> @RunWith(OpenEjbRunner.class)
> @TestContext
> public class BasicStatelessBeanTest
> {
>  @EJB
>  public BasicStatelessBeanLocal basicStatelessBean;
>
>  @Resource
>  public InitialContext initialContext;
>
>  @Test
>  public void testSomeMethod() {}
> }
>
> It then runs the test and injects any @EJB annotated fields, and any
> InitialContext type fields when they're annotated with @Resource.
>
> It's very very basic though. Took me probably about 30 minutes to put
> it together. The injection is a JNDI brute force method, where it
> tries different combinations of names, depending on the annotation
> arguments and field type. Further you can supply a properties resource
> filename to @TestContext for customized InitialContext initialization.
> It also only supports Local initial context (UNLESS you specify
> mappedName in each @EJB annotation).
>
> Note that the test runner should initialize the InitialContext for
> JUnit 3 tests (tests extending "TestCase"), though it won't do any
> injection, so it's pretty useless. I have yet to figure out how the
> Runner works for JUnit 3 tests. Once I'm able to intercept the test
> class instance the rest should be easy.
>
> None of the above limitations is hard to fix, and I'm definitely going
> to do them. Like I said, this is my result after playing with it for
> about half an hour.
>
> Further, I'm using reflection to do the injection, so the fields need
> to be public.
>
> Well, I'm using this way from now on. Our tests are already strapped
> with a small framework I made, which wraps OpenEJB and is configured
> through Spring, so it allows you to customize your lookups nicely,
> even overriding certain EJB lookups with spring beans. If anyone wants
> some more of this, just let me know and I'll send it along. I'll be
> evolving the for our own purposes, and if there's any interest I'm
> willing to send updates.
>
> Note that whatever I send is licensed under Apache 2.0, so you're free
> to use it in whichever way the license allows.
>
> Attached is the maven project for the runner. It contains tests to
> demonstrate it's use. If the attachment was rejected by the mailing
> list, just reply and I'll find another way to distribute it.
>
> Quintin Beukes
>

Reply via email to