users@glassfish.java.net

Confused about unit testing of managed and session beans

From: <glassfish_at_javadesktop.org>
Date: Sun, 22 Feb 2009 15:40:01 PST

I read about the goodness of the EJBContainer class in EJB 3.1 (which I assume will make it into GFv3, right?) Here is from http://www.slideshare.net/pelegri/ejb-31-and-glassfish-v3-prelude-presentation, with minor modification for unit testing:

public class BankTester {
  @Test public testSolvency() {
    EJBContainer container = EJBContainer.createEJBContainer();
    BankBean bank = (BankBean) container.getContext().lookup("java:global/bank/BankBean");
    bank.buy(new ToxicAsset());
    bank.payCEO();
    assertTrue(bank.isSolvent());
    container.close();
  }
}

That's not bad, but it isn't good either. Is there anything in the EJB 3.1 API that lets me write a JUnit extension so a test case can look like this:

public class BankTester {
  @EJB private BankBean bank;
  @Test public testSolvency() {
    bank.buy(new ToxicAsset());
    bank.payCEO();
    assertTrue(bank.isSolvent());
  }
}

For this to work, I'd instantiate a container and a BankTester. Then I'd like to call container.inject(tester), so that the container does what it already knows to do, namely inject into annotated fields or setters. I couldn't find anything like that in the EJB 3.1 javadoc.

The other reason I'd like such a feature is to test JSF managed beans that contain references to session beans (or for that matter, any other resource that the container is willing to inject.)

I haven't seen anything on unit testing managed beans with the embedded container, not even slideware. What is a good approach? (I had a peek at JSFUnit, but that seemed rather heavyweight. I just want to test my managed beans as POJOs.)

Thanks,

Cay
[Message sent by forum member 'cayhorstmann' (cayhorstmann)]

http://forums.java.net/jive/thread.jspa?messageID=333274