Spring creates a virtual DB environment for a single test

Source: Internet
Author: User
During local DAO tests or unit tests, the db environment is sometimes difficult to implement, and even if the database environment is called, sometimes the database does not need to be filled with useless data.

During local DAO tests or unit tests, the db environment is sometimes difficult to implement, and even if the database environment is called, sometimes the database does not need to be filled with useless data.

During local DAO tests or unit tests, the db environment is sometimes difficult to implement, and even if the database environment is called, sometimes it does not want to add useless data to the db, spring provides a virtual db environment to facilitate unit testing without interfering with the real db environment.

Configure in spring environment:




Type supports HSQL, H2, and Derby. The default value is HSQL.
The schema. SQL file is the statement used to create a database and data. SQL is the table data file. You can insert some data first.

After configuring the embedded database, you can inject it into the DAO that requires dataSource, and then you can perform the DAO unit test.

In addition, there is a code method to create an 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 ();

Related reading:

Use Spring annotations to implement Netty server-side UDP applications

Spring2.5.6 + Hibernate3 + RMI Integration

Steps for integrating Struts2 and Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.