The path of continuous integration--unit test of data access layer

Source: Internet
Author: User
Tags naming convention

Look over the article before found that the last record of continuous integration is 3 years ago, and only recorded two, is really ashamed. However, the continuous integration of the flame is always burning in the heart, hope this beginning can be some breakthrough.

Testing is the cornerstone of continuous integration, and integration without testing is essentially meaningless. How to write a good test is the first question that lies before me. Let's start with the data access layer. Funny to say, starting with a continuous integration for the first time 3 years ago, you start to consider some of the problems with testing the data access layer:

Do I have to install a MySQL on the test server? What happens when the database structure changes. How to eliminate the dependencies between tests. How the test data is managed. Besides, there is so much logic in the test data. How the results are validated.
......

These problems linger in my mind for a long time, "The Master of a generation" said "better in a think into, MO in a thought stop", in time to break the head, as direct practice. That's a question.

Before continuing, explain the architecture of the current program, the classic Spring + Spring Data + Hibernate + MySQL, so the following solution is based on this architecture. In addition, the program is built from MAVEN.

First, use the memory database instead of MySQL

I chose the HSQLDB, the official web has many examples to refer to. The HSQLDB offers several different usage patterns, where only memory mode is selected. Spring provides support for embedded databases through <jdbc:embedded-database> tags, and the configuration of data sources in Applicationcontext-test.xml is simple:

<jdbc:embedded-database id= "DataSource" type= "Hsql"/>

Hsql does not need to be installed, the jar package is introduced as a dependency in Pom.xml:

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactid>hsqldb</artifactid >
            <version>2.2.8</version>
            <scope>test</scope>
        </dependency>

Second, the maintenance of database structure

During the development of the project, the Flyway is used to maintain the change of database structure. Although spring also provides the opportunity to execute SQL via <jdbc:initialize-database>, it has been tested and unable to complete the flyway tasks. This begins to think about whether you must choose Flyway and control the structural changes through SQL. Flyway mainly refers to Ruby's DB migration mechanism, with each modification being based on the previous version, which does not affect the running logic. However, there is no need to use Flyway to control the development phase, and the maintenance of SQL also takes effort. So he turned to the hibernate support for DDL, and then the following configuration:

	<!--Jpa Entity Manager configuration-->
	<bean id= "entitymanagerfactory" class= "
		Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ">
		<property name=" DataSource "ref=" DataSource "/> <property name= jpavendoradapter" ref= "Hibernatejpavendoradapter"
		></property>
		<property name= "Packagestoscan" value= "Com.noyaxe.myapp"/> <property name=
		"Jpaproperties" >
			<props>
				<!--naming convention my_name->myname-->
				<prop key= "Hibernate.ejb.naming_strategy" > org.hibernate.cfg.improvednamingstrategy</prop>
				<prop key= "Hibernate.show.sql" >true</prop >
                                <prop key= "Hibernate.hbm2ddl.auto" >update</prop>
			</props>
		</property >
	</bean>

But for Hibernate's DDL support, I was skeptical: 1. If data already exists in the database, then how the field type changes will be handled. 2. How to better maintain the DDL changes.

adjourned


fill in the record:

First of all thank Csfreebird's advice, let me stop to think why to choose hsqldb behind the problem. The following articles for your reference, especially the last article is worth thinking carefully:

Testing Databases with JUnit and Hibernate part 1:one to rule them

Database unit Testing with DBUnit, Spring and TestNG

Basic mistakes in Database testing<script type= "Text/javascript" >var _bdhmprotocol = ("https:" = Document.location.protocol)? "https://": "http://"); document.write (unescape ("%3cscript src= '" + _bdhmprotocol + "hm.baidu.com/h.js%3f94beed115f7adca9895daf1cc025ad4f") Type= ' Text/javascript '%3e%3c/script%3e));</script>

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.