All Articles

Test EJB Local Outside Container

Dari pada capek harus deploy dulu ke application server container, trus di lookup session beannya dari client, lalu dijalankan, kalo gagal pun ribet, harus modifikasi kode, compile, deploy ulang lagi, wasting time banget. Maka dari itu saya melakukan unit test di local aja. Kalau semua test case udah muncul ijo2 baru deh di deploy, bisa tenang dan confidence.

Langkah2nya :

  1. Buat persistence unit (persistence.xml) yg menggunakan configurasi jdbc local, beri nama beda configurasi menggunakan datasource di application server. Contoh berikut menggunakan toplink dan oracle xe
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="opencore_dev">
    <jta-data-source>jdbc/opencoreDS</jta-data-source>
  </persistence-unit>
  <persistence-unit name="opencore_dev_local">
    <class>id.co.sigma.core.eod.entity.LoanAccount</class>
    <class>id.co.sigma.core.eod.entity.EodDetail</class>
    <class>id.co.sigma.core.eod.entity.EodHeader</class>
    <class>id.co.sigma.core.eod.entity.EodHistory</class>
    <properties>
      <property name="toplink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
      <property name="toplink.jdbc.url"
                value="jdbc:oracle:thin:@IPDATABASESERVER:SID"/>
      <property name="toplink.jdbc.user" value="xxxxxxxx"/>
      <property name="toplink.jdbc.password"
                value="passwordacak2acak2"/>
      <property name="toplink.target-database" value="Oracle"/>
      <property name="toplink.logging.level" value="FINER"/>
    </properties>
  </persistence-unit>
</persistence>
  1. Create EntityManager dan set ke SessionBean yg akan ditest
public abstract class BaseSessionTestCase extends TestCase {
    protected EntityManager getEntityManager() throws Exception {
        // opencore_dev_local persistence unit konfigurasi jdbc local yg td didefenisikan
        return Persistence.createEntityManagerFactory("opencore_dev_local").createEntityManager();
    }
    
    public void setUp() throws Exception {
       getSessionBean().setEm(getEntityManager());
    }
    
    protected abstract AbstractBaseSession getSessionBean();
}

public class LoanBillingEJBTest extends BaseSessionTestCase {
    ILoanBillingEJB sessionBean
    
    public void setUp() throws Exception {
        sessionBean = new LoanBillingEJB();
        super.setUp();
    }
    protected AbstractBaseSession getSessionBean() {
        return (AbstractBaseSession)sessionBean;
    }
}
  1. Ya udah selesai, tinggal dijalankan tu sessionBean.

next time, saya rencana nulis tentang Abstract Session Bean, BaseInterface, Generic, biar ga bolak balik mendefenisikan perilaku umum session bean.