Methods of mastering and using Spring Unit test framework

Source: Internet
Author: User
Keywords Unit testing Framework testing
Tags aliyun analysis based configuration continuous development directory directory structure

Unit testing and integration testing play a pivotal role in the entire process of our software development, on the one hand, programmers to write unit tests to verify the effectiveness of their programs, on the other hand, managers through continuous automated unit testing and analysis of unit testing coverage to ensure the quality of the software itself. Here, let's not talk about the importance of unit testing itself, but for most of today's java-based http://www.aliyun.com/zixun/aggregation/13760.html "> Enterprise applications, Spring has become a standard configuration , on the one hand, it realized the low coupling between programs, and also through some configuration to reduce the enterprise software integration workload, such as Hibernate, Struts and other integration. So, there's a question, how do we do unit testing in applications that generally use Spring? Or how can we effectively implement various unit tests in the Spring ecosystem? That's what this article is about to tell you.

Unit tests the main framework currently includes Junit, TestNG, and some MOCK frameworks, such as Jmock, Easymock, Powermock, which are all powerful tools for unit testing, but are they efficient when used in the Spring development environment? Fortunately, Spring provides powerful support for unit testing, including:

Support for mainstream test frameworks Junit and TestNG support for using dependency injection in test classes denpendency injection support for automated transaction management of test classes support the use of various annotation tags to improve development efficiency and code simplicity Spring 3.1 Support for using non-XML configuration methods and profile based bean configuration patterns in test classes

By reading this article, the reader can quickly grasp the test method based on the Spring TestContext framework, and understand the basic principle of implementation. This article will provide a number of ways to use test labels that allow developers to significantly reduce their coding effort. OK, now let's start the Spring Test tour!

So how did we do it?

Here is a unit test based on Junit, which runs in a spring-based application and needs to be tested using the associated configuration file for spring. The related class diagram is as follows:

Database tables

Suppose there is an employee Account table that holds the employee's basic account information, and the table structure is as follows:

ID: integer type, unique identifier NAME: string, Login account SEX: string, Sex Age: String, ages

Suppose the table is already built and the content is empty.

The jars you need to introduce include:

Cglib-nodep-2.2.3.jar Commons-logging.jar Hsqldb.jar Junit-4.5.jar Log4j-1.2.14.jar Spring-asm-3.2.0.M1.jar Spring-beans-3.2.0.m1.jar Spring-context-3.2.0.m1.jar Spring-core-3.2.0.m1.jar Spring-expression-3.2.0.M1.jar Spring-jdbc-3.2.0.m1.jar Spring-test-3.2.0.m1.jar Spring-tx-3.2.0.m1.jar Testng-6.8.jar

The HSQLDB is our test database.

Test engineering directory structure and dependent jar packs

In Eclipse, we can expand the engineering directory structure and see the engineering directory structure and the list of dependent jar packages as shown in the following illustration:

Figure 1. Engineering directory Structure

Related Article

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.