Spring建立虛擬DB環境進行單測

來源:互聯網
上載者:User

在進行DAO本地測試或者單元測試時,db環境有時很難弄,而且即使調通了db環境,有時也不想往db裡面灌些無用資料,所以,為了不對真實db環境造成侵入,spring提供了一套虛擬db環境,利用它可以方便地進行單元測試。

在spring環境中配置:

<jdbc:embedded-database id="db_product_recommend" type="H2">
<jdbc:script. location="classpath:database/product-recommend-schema.sql" />
<jdbc:script. location="classpath:database/product-recommend-data.sql" />
</jdbc:embedded-database>

type支援HSQL、H2、Derby三種。預設為HSQL
schema.sql檔案為建庫建表語句,data.sql為表資料檔案,初始可以先insert一些資料。

配置embedded database之後,就可以將其注入到需要dataSource的DAO中,之後就可以進行DAO單元測試了。

另外,還有一種代碼方式來建立embedded database:
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.type(H2).script("schema.sql").script("test-data.sql").build();
// do stuff against the db (EmbeddedDatabase extends javax.sql.DataSource)
db.shutdown();

運用Spring註解實現Netty伺服器端UDP應用程式

Spring2.5.6+Hibernate3+RMI整合

Struts2與Spring整合步驟

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.