Abstract MockEJB

本文介绍了一个用于测试EJB应用程序的抽象基类AbstractMockEJBTest。该类通过部署会话Bean和绑定数据源来模拟EJB环境,适用于单元测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here is a abstract class to extends MockEJB, it's very useful and powerful for testing ejb.

  1. package com.test.test.web.test.action;
  2. import javax.naming.Context;
  3. import javax.naming.InitialContext;
  4. import junit.framework.TestCase;
  5. import org.mockejb.MockContainer;
  6. import org.mockejb.SessionBeanDescriptor;
  7. import org.mockejb.jndi.MockContextFactory;
  8. import com.sybase.jdbc2.jdbc.SybDataSource;
  9. public abstract class AbstractMockEJBTest extends TestCase {
  10.     private Context context;
  11.     private MockContainer mockContainer;
  12.     /**
  13.      * Initial context
  14.      */
  15.     protected void setUp() throws Exception {
  16.         MockContextFactory.setAsInitial();
  17.         context = new InitialContext();
  18.         mockContainer = new MockContainer(context);
  19.     }
  20.     /**
  21.      * Retrieve the context
  22.      * 
  23.      * @return context
  24.      */
  25.     protected Context getContext() {
  26.         return context;
  27.     }
  28.     /**
  29.      * Deploy the SessionBean to the mock container
  30.      * 
  31.      * @param jndi
  32.      *            Jndi name
  33.      * @param home
  34.      *            Home interface
  35.      * @param remote
  36.      *            Remote interface
  37.      * @param bean
  38.      *            Concrete class
  39.      * @throws Exception
  40.      */
  41.     public void deploySessionBean(String jndi, Class home, Class remote, Class bean) throws Exception {
  42.         SessionBeanDescriptor descriptor = new SessionBeanDescriptor(jndi, home, remote, bean);
  43.         mockContainer.deploy(descriptor);
  44.     }
  45.     /**
  46.      * Bind datasource to the context
  47.      * 
  48.      * @param jndi
  49.      *            Jndi name
  50.      * @param serverName
  51.      *            Server name
  52.      * @param port
  53.      *            port
  54.      * @param databaseName
  55.      *            Database name
  56.      * @param user
  57.      *            user
  58.      * @param password
  59.      *            password
  60.      * @throws Exception
  61.      */
  62.     public void bindDataSource(String jndi, String serverName, int port, String databaseName, String user, String password) throws Exception {
  63.         SybDataSource ds = new SybDataSource();
  64.         ds.setServerName(serverName);
  65.         ds.setPortNumber(port);
  66.         ds.setDatabaseName(databaseName);
  67.         if (user != null)
  68.             ds.setUser(user);
  69.         if (password != null)
  70.             ds.setPassword(password);
  71.         context.rebind(jndi, ds);
  72.     }
  73.     public void tearDown() {
  74.         MockContextFactory.revertSetAsInitial();
  75.     }
  76. }

 

How to use:

  1. public class EJBTest extends AbstractMockEJBTest {
  2.     private TestDAOLocalHome testDAOHome;
  3.     private TestDAOLocal testDAOLocal;
  4.     protected void setUp() throws Exception {
  5.         super.setUp();
  6.         super.deploySessionBean("test/biz@version@/ejb/test/TestDAOLocal", TestDAOLocalHome.class, TestDAOLocal.class, TestDAOEJB.class);
  7.         super.bindDataSource("test/jdbc/TESTDB""192.168.0.1"1000"TEST""test""test");
  8.         testDAOHome = (TestDAOLocalHome) super.getContext().lookup("test/biz@version@/ejb/test/TestDAOLocal");
  9.         testDAOLocal = testDAOHome.create();
  10.     }
  11.     public void testDAO() throws Exception {
  12.         Map returnMap = testDAOLocal.getCountryMap();
  13.         assertNotNull(returnMap);
  14.         assertEquals("CHINA", returnMap.get("CN"));
  15.     }
  16. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值