Versions Compared

Key

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

...

@Resource annotation of a field

No Formatcode
    @Resource
    int maxLineItems = 10;

Injection through a setter method

...

@Resource annotation of a setter method

No Formatcode
 
    @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 Formatcode
    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 Formatcode
    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:

...