EJB Unit Testing

I am looking for a way to apply TDD to a Beans session.

can anyone provide tips and links to unit test them?

How to use JUnit for this?

PS: I am new to development testing and Beans session.

I am using EJB v2.

+6
java java-ee junit tdd ejb
source share
4 answers

I assume you are talking about an EJB2.x Beans session. For these animals, I like:

  • Use a Bean session as a wrapper that simply delegates POJO logic, which you can easily test outside the container. Testing external containers is better, faster, easier, etc., but will not cover things like checking deployment descriptor - and / or -
  • Use something like Cactus to test inside a container (check out Howto EJB ) - and / or -
  • Build and deploy an EJB module with Cargo to test integration.
+8
source share

You do not say which version of EJB you are using. If it is EJB v3, check out Ejb3Unit . On the website:

Ejb3Unit is a JUnit extension and can perform automatic stand-alone junit tests for all EJB 3.0 compliant Java EE projects. An out-of-competition approach to testing leads to short cycles of building tests, because container deployment is not necessary anymore.

However, I would recommend separating functionality from EJB specifics. This will allow you to test complex functionality outside the container and without the use of frameworks like the one above. Most of your tests will test POJOs (plain old Java objects), and relatively little focus on testing the persistence platform.

EDIT: So, if you are using EJB v2, then obviously ignore the first point. However, the second point remains valid.

+5
source share

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 .

+2
source share

Mockrunner can be used in conjunction with MockEJB to write tests for EJB-based applications.
Take a look at this
http://mockrunner.sourceforge.net/examplesejb.html

0
source share

All Articles