Avoiding EJB Testing
Problem
You want to test server-side business logic but avoid the complexity of EJB deployment and remote method calls.
Solution
Decouple business logic from EJBs, facilitating dedicated unit testing without an application server.
Discussion
Example 11-2 shows a code fragment from a stateless session bean. In order to test this bean, you must deploy it to an application server. The deployment steps make your Ant buildfile more complex and slow you down as you write tests, deploy, run your tests, and change your code.
Example 11-2. Part of a stateless session bean
public class AccountBean implements SessionBean {
// a bunch of EJB methods
public void ejbCreate( ) { ... }
public void ejbActivate( ) { ... }
public void ejbPassivate( ) { ... }
etc...
public double getBalanceAsOf(Date date) {
// fetch data
...
// apply interest
...
// deduct bank fees
...
}
}Fortunately, we can refactor EJBs so they do not require as much testing. If business logic is decoupled from the beans, you can write tests against the business logic without first deploying to an application server. This capability drastically simplifies your tests, while making the whole test-driven development cycle faster.
Example 11-3 shows the improved bean.
Example 11-3. Refactored stateless session bean
public class AccountBean implements SessionBean {
// a bunch of EJB methods
public void ejbCreate( ) { ... }
public void ejbActivate( ) { ... }
public void ejbPassivate( ) { ... }
etc...
public double getBalanceAsOf(Date ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access