Overview
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, for most Java-based enterprise applications today, Spring has become a standard configuration, on the one hand, it realizes the low coupling between programs, but also through some configurations to reduce the workload of enterprise software integration, such as and Hibernate, Struts, etc. integration. So, there's a question, how do we do unit testing in applications that generally use Spring? Or, how can we efficiently 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 the following key features:
Support for mainstream test frameworks Junit and TestNG
Supports the use of dependency injection in test classes denpendency injection
Support for automated transaction management of test classes
Supports the use of various annotation tags to improve development efficiency and code simplicity
Spring 3.1 supports the use of non-XML configuration methods and profile based bean configuration patterns in test classes
By reading this article, you can quickly grasp the test methods based on the Spring TestContext framework and understand the basic implementation principles. This article will provide a number of ways to use test labels, which allow developers to significantly reduce their coding effort. OK, now let's start the Spring Test tour!
So, how did we do that?
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, gender
Age: Strings, ages
Suppose the table is already built and the content is empty.
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:
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
One of the HSQLDB is our test database.
Figure 1. Engineering directory Structure