I'm looking for a way to apply TDD to Session Beans.
can anyone provide advices and links on how to unit test them ?
how to use JUnit to do so ?
P.S : I'm new to Test Driven Development and Session Beans.
I'm using EJB v2.
I'm looking for a way to apply TDD to Session Beans.
can anyone provide advices and links on how to unit test them ?
how to use JUnit to do so ?
P.S : I'm new to Test Driven Development and Session Beans.
I'm using EJB v2.
You don't say which version of EJB you're using. If it's EJB v3, check out Ejb3Unit. From the website:
Ejb3Unit is a JUnit extention and can execute automated standalone junit tests for all EJB 3.0 conform JEE projects. The out of container test approach leads to short build-test-cycles, because no container deployment is necessary anymore.
However I would advocate separating out functionality from the EJB-specifics. This will allow you to test complex functionality outside the container and without using frameworks such as the above. The majority of your tests would be testing POJOs (plain old Java objects) and relatively few would focus on your persistence framework testing.
EDIT: So if you're using EJB v2 then obviously ignore the first point. The second point remains valid, however.
I'm assuming you are talking about EJB2.x Session Beans. For these kind of animals, what I like to do is:
I'm currently using apache openejb as an embedded container for unit tests. Although this is an EJB3 / JPA project it should work the same for EJB2. To bootstrap the container in your tests you just have to create an InitialContext object that you can later use to lookup 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");
Edit: There is some more documentation in the 'Testing Techniques' section at http://openejb.apache.org/3.0/index.html.