您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
hibernate+junit+Middlegen入門
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2012/12/28 14:00:11 ] 推薦標(biāo)簽:

然后整理得:
 C:hibernateMiddlegen-Hibernate-r5uildgen-srcHibernateSamplehibernate>dir
 驅(qū)動(dòng)器 C 中的卷是 本地磁盤
 卷的序列號(hào)是 08DF-03E4

C:hibernateMiddlegen-Hibernate-r5uildgen-srcHibernateSamplehibernate 的目錄

2005-03-22 19:04 <DIR> .
 2005-03-22 19:04 <DIR> ..
 2005-03-22 19:04 865 Book.hbm.xml
 2005-03-22 19:01 1,148 Book.java
 2 個(gè)文件 2,013 字節(jié)
 2 個(gè)目錄 1,528,754,176 可用字節(jié)

3.應(yīng)用hibernate
 在classpath配置 hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd";>
<hibernate-configuration>
<!-- SessionFactory 配置 -->
<session-factory>
<!-- 數(shù)據(jù)庫(kù)URL -->
<property name="hibernate.connection.url">
jdbc:jtds:sqlserver://127.0.0.1:1433/testbook
</property>
<!-- 數(shù)據(jù)庫(kù)JDBC驅(qū)動(dòng) -->
<property name="hibernate.connection.driver_class">
net.sourceforge.jtds.jdbc.Driver
</property>
<!-- 數(shù)據(jù)庫(kù)用戶名 -->
<property name="hibernate.connection.username">sa</property> <!-- 數(shù)據(jù)庫(kù)用戶密碼 -->
<property name="hibernate.connection.password">zh1107</property>
<!--dialect ,每個(gè)數(shù)據(jù)庫(kù)都有其對(duì)應(yīng)的Dialet以匹配其平臺(tái)特性 -->
<property name="dialect">
net.sf.hibernate.dialect.SybaseDialect </property>
<!-- 是否將運(yùn)行期生成的SQL輸出到日志以供調(diào)試 --> <property name="hibernate.show_sql">True</property>
<!-- 是否使用數(shù)據(jù)庫(kù)外連接 --> <property name="hibernate.use_outer_join">True</property>
<!-- 事務(wù)管理類型,這里我們使用JDBC Transaction --> <property name="hibernate.transaction.factory_class"> net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<!--映射文件配置,注意配置文件名必須包含其相對(duì)于根的全路徑-->
<mapping resource="HibernateSample/hibernate/Book.hbm.xml" /> </session-factory>
</hibernate-configuration>

還有把hibernate-2.1下lib得.jar包引到classpath里。

 Junit的測(cè)試,注意要junit包。HibernateTest.java
/*
 * Created on 2005-3-20 dahe
 */
package test;import java.util.List;
import net.sf.hibernate.*;
import junit.framework.*;
import HibernateSample.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateTest extends TestCase {
 Session session = null;
 /**
  * JUnit中setUp方法在TestCase初始化的時(shí)候會(huì)自動(dòng)調(diào)用 一般用于初始化公用資源 此例中,用于初始化Hibernate Session
  */
  protected void setUp() {
   try {
     /**
     * 采用hibernate.properties配置文件的初始化代碼: Configuration config = new
     * Configuration(); config.addClass(TUser.class);
     */
     // 采用hibernate.cfg.xml配置文件
     // 請(qǐng)注意初始化Configuration時(shí)的差異:
     // 1.Configuration的初始化方式
     // 2.xml文件中已經(jīng)定義了Mapping文件,因此無(wú)需再Hard Coding導(dǎo)入
     // POJO文件的定義
     Configuration config = new Configuration().configure();
     SessionFactory sessionFactory = config.buildSessionFactory();
     session = sessionFactory.openSession();
     } catch (HibernateException e) {
     e.printStackTrace();
     }
 }
 /**
  * * 與setUp方法相對(duì)應(yīng),JUnit TestCase執(zhí)行完畢時(shí),會(huì)自動(dòng)調(diào)用tearDown方法 一般用于資源釋放 * 此例中,用于關(guān)閉在setUp方法中打開(kāi)的Hibernate Session
  */
 protected void tearDown() {
  try { session.close();
  } catch (HibernateException e) {
    e.printStackTrace();
  }
 }
 /**
 * 對(duì)象持久化(Insert)測(cè)試方法
 *
 * JUnit中,以”test”作為前綴的方法為測(cè)試方法,將被JUnit自動(dòng)添加 到測(cè)試計(jì)劃中運(yùn)行
 */
 public void testInsert() {
  try {
     Book user = new Book();
     user.setBook("Emma");
     user.setSn("asdfd");
     Transaction tran=session.beginTransaction();
     session.save(user);
     tran.commit();
     } catch (HibernateException e) {
       e.printStackTrace();
       Assert.fail(e.getMessage());
     }
 }
 /**
 * 對(duì)象讀。⊿elect)測(cè)試 請(qǐng)保證運(yùn)行之前數(shù)據(jù)庫(kù)中已經(jīng)存在name=’Erica’的記錄
 */
 public void testSelect() {
  String hql = " from Book where book='Emma'";
  try {
     List userList = session.find(hql);
     Book user = (Book) userList.get(0);
     Assert.assertEquals(user.getBook(), "Emma");
     } catch (HibernateException e) {
       e.printStackTrace();
      Assert.fail(e.getMessage());
     }
   }
 }

測(cè)試通過(guò)

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd