我希望使用嵌入式Glassfish容器和JUnit对我的Java EE代码进行单元测试。它本身就可以正常工作,但我发现当调用Primefaces EventBusFactory时它会因Null Pointer Exception而失败。我不知道如何解决这个问题,或者我是否应该以另一种方式对此进行测试。
这是我的测试:
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");
}
}
这是我在RequestScoped有状态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");
}