开发者

OpenEJB alternate descriptors fail to work when using a jUnit test suite

开发者 https://www.devze.com 2023-04-08 16:17 出处:网络
I have managed to get alternate descriptors to work with my unit-tests running on OpenEJB using stubs for dependant EJB components, when each test is executed on their own. But once I introduce a test

I have managed to get alternate descriptors to work with my unit-tests running on OpenEJB using stubs for dependant EJB components, when each test is executed on their own. But once I introduce a test suite, it seems that deployment descriptor is taken from the first test added to the suite.

Some code to explain it better. Beans under test are something like

@Stateless
@Local(A.class)
public class ABean implements A {
     // Bean implementation, no dependencies
}


@Stateless
@Local(B.class)
public class BBean implements B {

     @EJB开发者_Python百科
     A aBean;  // Dependency to ABean

     // Rest of the implementation
}

And testcase for B (testcase for A is similar, except it does not set the property for using alternate descriptor)

public class BBeanTest {
    private B bean;

    @Before
    public void bootContainer() throws Exception {
        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY,
               "org.apache.openejb.client.LocalInitialContextFactory");
        props.put("openejb.altdd.prefix", "test");  // Use stubs

        System.out.println("boot B: " + props);

        context = new InitialContext(props);
        bean = (B) context.lookup("BBeanLocal");
    } 
}

And as said, this all works just fine when executed alone. The alternate descriptor injects a stub implementation of A interface.

When using the following test suite, things start to fall apart.

 @RunWith(Suite.class)
 @Suite.SuiteClasses({
   ABeanTest.class, 
   BBeanTest.class
 })
 public class MySuite {
      // Empty on purpose, annotations do the trick
 }

When running this suite, the alternate descriptor for testing B is not taken into use. Although, the output shows that at least the property is set before each test

 boot A: {java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory}
 boot A: {java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory}
 boot A: {java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory}
 boot B: {java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory, openejb.altdd.prefix=test}
 boot B: {java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory, openejb.altdd.prefix=test}

If I reverse the order of loading tests to suite, i.e. add BBeanTest.class before ABeanTest.class, it'll use the alternate descriptor. As the ABean has no dependencies, this'll work fine in this case, but probably causes problems with bigger setups with multiple alternate descriptors.

Any pointers?

Thanks in advance.

EDIT Based on the log output, the container is actually booted only once for the first test as it takes approx. 2,5 seconds to execute while the others take around 0,001 seconds.

EDIT2 OpenEJB version is Apache OpenEJB 3.1.4 build: 20101112-03:32


Based on the log output, the container is actually booted only once for the first test as it takes approx. 2,5 seconds to execute while the others take around 0,001 seconds.

As you rightly noticed, the initialization happens only once.


@RunWith(Suite.class)
 @Suite.SuiteClasses({
   ABeanTest.class, 
   BBeanTest.class
 })

Hence in this case, both ABeanTest and BBeanTest ran within the same container instance, with the same initial context properties as set by ABeanTest.

In your case, since you need different settings for the two test classes, I think dumping the container instance at ABeanTest @AfterClass and using a new one in BBeanTest should do it.

This blog post shows how

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号