Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • "A field or method of a bean class may be annotated to request that an entry from the bean's environment
    be injected. Any of the types of resources or other environment entries^88^ entries described in this chapter may
    be injected. Injection may also be requested using entries in the deployment descriptor corresponding to
    each of these resource types."

...

No Format
 
    @Resource
    public void setMaxLineItems(int maxLineItems) {
        this.maxLineItems = maxLineItems;
    }

...

JUnit Test

Writing an JUnit test for this example is quite simple. We need just to write a setup method to create and initialize the InitialContext, and then write our test methods.

Test fixture

No Format

    protected void setUp() throws Exception {
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
        properties.setProperty("openejb.deployments.classpath.include", ".*resource-injection.*");

        initialContext = new InitialContext(properties);
    }

Test methods

No Format

    public void testAddLineItem() throws Exception {
        Invoice order = (Invoice) initialContext.lookup("InvoiceBeanBusinessRemote");
        assertNotNull(order);
        LineItem item = new LineItem("ABC-1", "Test Item");

        try {
            order.addLineItem(item);
        } catch (TooManyItemsException tmie) {
            fail("Test failed due to: " + tmie.getMessage());
        }
    }

Running

Running the example is fairly simple. Just execute the following commands:

...