I am currently using apache openejb as a built-in container for unit tests. Although this is an EJB3 / JPA project, it should work the same for EJB2. To load a container into your tests, you just need to create an InitialContext object, which you can later use to search for EJBs and DataSources:
Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); // a DataSource named "mysql" props.put("mysql", "new://Resource?type=DataSource"); props.put("mysql.JdbcDriver", "com.mysql.jdbc.Driver"); props.put("mysql.JdbcUrl", "jdbc:mysql://localhost:3306"); props.put("mysql.JtaManaged", "true"); props.put("mysql.DefaultAutoCommit", "false"); props.put("mysql.UserName", "root"); props.put("mysql.Password", "root"); Context context = new InitialContext(props); LocalInterface local = (LocalInterface)context.lookup(localInterfaceName + "BeanLocal"); DataSource ds = (DataSource)context.lookup("java:openejb/Resource/mysql");
Change There are several more documents in the Test Methods section: http://openejb.apache.org/3.0/index.html .
Jรถrn horstmann
source share