OpenEJB Local Client Injection Fails

2020-07-17 04:47发布

问题:

Note that I'm mirroring the example given here very closely.

In fact, my situation is somewhat simpler as I'm not even testing with a persistence unit at this point. My test project provides a simple MDB and a session bean; both the MDB and the session bean are getting loaded as normal, and can be successfully tested (in a constrained fashion) without injection.

The suggested injection with the @LocalClient annotation on my unit tests is failing with the known error:

javax.naming.NamingException: Unable to find injection meta-data for [your-class]. Ensure that class was annotated with @org.apache.openejb.api.LocalClient and was successfully discovered and deployed. See http://openejb.apache.org/3.0/local-client-injection.html

When I visit this page it informs me that I may need to add an extra property to my test case context setup. So that now looks like:

@Override
public void setUp() throws Exception {
    initializeContext();
}
public void initializeContext() {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
    // the property i've added
    p.put("openejb.tempclassloader.skip", "annotations");

    try {
        InitialContext initialContext = new InitialContext(p);
        initialContext.bind("inject", this);
    } catch (Throwable throwable) { 
        throwable.printStackTrace();
        throw new RuntimeException(throwable);
    }
}

But it's still failing. I really like this idiom and would be very excited if I could successfully use it in my projects.

A few other notes:

  • I am providing an 'empty' ejb-jar.xml (in src/main/resources) and an application-client.xml (in src/test/resources) as suggested by Apache to tell OpenEJB to scan the classpath [UPDATE: as it turns out, I was doing this wrong. See my answer below for the suggestion that worked for me.]
  • The test cases annotated with @LocalClient aren't identified by the OpenEJB engine as actually getting picked up and processed properly (as my MDBs are, for example)

Thanks in advance for any help or guidance.

回答1:

This issue is likely caused by improper location of the descriptors which hint OpenEJB which sorts of modules are available.

To ensure the test-classes get picked up properly, make sure you're placing a file named application-client.xml at src/test/resources/META-INF with the following content:

<application-client/>

This should force OpenEJB to scan and react to the presence of @LocalClient annotations.



回答2:

I had a similar issue when I tried to test stuff in a test project called tomee-embedded-trial and it turned out that openejb ignores stuff called tomee-.* .

I fixed it for me by specifying the following system properties: openejb.deployments.classpath.include=".*-trial.*" openejb.deployments.package.include=".*-trial.*"