views:

561

answers:

3

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.

+4  A: 

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.

Brian Agnew
+6  A: 

I'm assuming you are talking about EJB2.x Session Beans. For these kind of animals, what I like to do is:

  • Use the Session Bean as a wrapper that just delegates logic to a POJO that you can test easily outside a container. Outside container testing is better, faster, easier, etc but won't cover things such as deployment descriptor validation - and/or -
  • Use something like Cactus for in-container testing (check the Howto EJB documentation) - and/or -
  • Build and deploy your EJB module with Cargo for integration testing.
Pascal Thivent
+1 - For your last two suggestions, I haven't thought about Cactus in years. :)
James Black
Actually, I didn't use Cactus for years :)
Pascal Thivent
I tried to setup Cactus for use, but I don't really get it ! I feel like it's too difficult to setup. I'm using Eclipse and NetBeans.
Attilah
Cactus is not that hard. Did you try google: http://www.google.com/search?q=ejb+testing+cactus ? There are plenty of simple samples.
Pascal Thivent
+1  A: 

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.

Jörn Horstmann
can you provide me with a link to a good tutorial on this ? seems promising.
Attilah