Mockrunner can be used in conjunction with MockEJB to write tests for EJB based applications. MockEJB usually works with a real database. You can use the JDBC framework of Mockrunner instead of a database. There's an EJBTestModule which utilizes the MockEJB API and provides a few additional methods. You can work without using the EJBTestModule, but it makes life easier, if you use MockEJB in conjunction with Mockrunner.

This page provides a very simple example how to use the Struts test framework and the JDBCTestModule of Mockrunner in conjunction with MockEJB and the EJBTestModule. The release comes with a more complex example.

The following action takes a message parameter from the request and writes it to a database using a session bean called LogSessionBean.

public class LogAction extends Action
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) 
                                 throws Exception
        String message = request.getParameter("message");
        InitialContext initialContext = new InitialContext();
        Object home = 
        LogSessionHome logHome = 
        LogSession log = logHome.create();
        return mapping.findForward("success");


Here's the code of the LogSessionBean. The example only shows the logMessage() method. The complete bean implementation and all the other EJB stuff like the corresponding interfaces and some XDoclet tags for deploying the bean to JBoss are included in the release.

public class LogSessionBean implements SessionBean
    private SessionContext sessionContext;
    public void logMessage(String message)
        Connection connection = null;
        PreparedStatement statement = null;
            InitialContext context = new InitialContext();
            DataSource dataSource = 
            connection = dataSource.getConnection();
            statement = connection.prepareStatement
                                     ("insert into logtable values(?, ?)");
                                   new Timestamp(System.currentTimeMillis()));
            statement.setString(2, message);
        catch(Exception exc)
            throw new EJBException(exc.getMessage());
                if(null != statement) statement.close();
                if(null != connection) connection.close();
            catch(SQLException sqlExc)


Here's the test for the above example.

public class LogActionTest extends ActionTestCaseAdapter
    private JDBCTestModule jdbcModule;
    private EJBTestModule ejbModule;
    protected void setUp() throws Exception
        jdbcModule = createJDBCTestModule();
        ejbModule = createEJBTestModule();

    public void testLogActionSuccess()
        addRequestParameter("message", "testmessage");
        jdbcModule.verifySQLStatementExecuted("insert into logtable");
        jdbcModule.verifyPreparedStatementParameter("insert into logtable", 
                                                    2, "testmessage");


The example uses several test modules in conjunction. The easiest way to do this is to extend ActionTestCaseAdapter instead of BasicActionTestCaseAdapter and to create the other test modules using createJDBCTestModule() and createEJBTestModule().

The EJBTestModule performs the MockEJB setup. It sets the MockContextFactory as default initial context factory and creates a MockUserTransaction. You have to deploy your bean to the mock container. Mockrunner allows you to set the package with the EJB interfaces, so you do not have to provide all the interfaces when deploying the beans. You can also deploy the bean directly to the mock container of MockEJB.

In this example we use the simulated database environment of Mockrunner, so we don't need a real transaction. But we want to keep track if the transaction was committed or rolled back, so we use the MockUserTransaction implementation of Mockrunner.

You can use the MockEJB feature to work with the remote container and you can get a real transaction from the JTA implementation of your application server. You have to call

    setDelegateEnvironment((new InitialContext()).getEnvironment());

in the setUp() method before calling super.setUp().

Mockrunner will automatically work with a JTA transaction in this case. Refer to the MockEJB documentation for a detailed description how to setup the remote container.

Please note that you have to use the verify methods of the EJBTestModule when testing the state of the MockUserTransaction, because the MockUserTransaction is a simulated JTA transaction. If the tested code manages the transaction on its own using JDBC, you have to work with the transaction methods of JDBCTestModule. Refer to the JDBC examples.