0

I'm looking to unit test my Java EE code by using the embedded Glassfish container and JUnit. On its own this works ok, but I've discovered that when the Primefaces EventBusFactory is called it fails with a Null Pointer Exception. I'm not sure how to resolve that, or if I should be testing this in another way.

Here is my test:

public class CreateOrderBookTest {

private RequestBean requestBean;
private EJBContainer ejbContainer;
private Context ctx;

@Before
public void setUp() throws Exception {
    ejbContainer = EJBContainer.createEJBContainer();
    System.out.println("Opening the container" );
    ctx = ejbContainer.getContext();
}

@After
public void tearDown() throws Exception {
     ejbContainer.close();
     System.out.println("Closing the container" );
}

@Test
public void test() throws NamingException {
    RequestBean requestBean = (RequestBean) ctx.lookup("java:global/classes/RequestBean");
    assertNotNull(requestBean);

    requestBean.createBidOrder(50, 1.0, "CLCS", "00001");
    }

}

Here is the function I'm testing in a RequestScoped Stateful bean:

 public void createBidOrder(Integer amount, Double price, String memberId, String traderId)
    {
        BidOrder order = new BidOrder(amount,price, em.find(Member.class, memberId), 
                em.find(Trader.class, new Integer(traderId)));
        logger.log(Level.INFO, "RequestBean.createBidOrder Created new order with bidprice {0}", 
                new Object[]{price});

        em.persist(order);

        EventBus eventBus = EventBusFactory.getDefault().eventBus();
        eventBus.publish("/notify", "order");
    }
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
zobbo
  • 117
  • 2
  • 13
  • PF Version? JSF Impl and Version? – Kukeltje Jun 16 '16 at 20:18
  • The PF version is 5.3, JSF implementation is using GlassFish so I believe it to be Mojarra version 2 – zobbo Jun 17 '16 at 09:36
  • Check this: http://stackoverflow.com/questions/31985116/nullpointerexception-when-using-primepush – Kukeltje Jun 17 '16 at 09:40
  • I already have that configured in my web.xml. I believe the problem is related to using the embedded version of Glassfish. It works fine if I deploy to the standard version of Glassfish. I'd be interested to know how people are unit testing Java EE apps with primefaces. Thanks. – zobbo Jun 18 '16 at 14:25
  • http://stackoverflow.com/questions/16747175/how-to-test-jsf-application – Kukeltje Jun 18 '16 at 18:42
  • Thanks I'll take a look at arquillian. I got around the problem with the eventbus for now by just catching the EJBException in the unit test that is caused by the NPE. – zobbo Jun 19 '16 at 18:55

0 Answers0