When doing spring related tests, it is troublesome, if only with junit test, need not have the initialization of applicationcontext, efficiency comparison below, but also have shortcomings. See below for details
- Causes multiple spring container initialization problems
Depending on the invocation process of the JUnit test method, each test method executes creates an instance of the test case and calls the Setup () method. As a general rule, we initialize the spring container in the Setup () method, which means that if there are many test methods for the test case, the spring container is repeatedly initialized multiple times. Although it is not too slow to initialize the spring container, it is possible to perform time-consuming operations such as loading the hibernate mapping file when the spring container is initialized, and if the spring container must be initialized repeatedly for each test method, the impact on the performance of the test is not negligible;
With the spring test suite, the spring container is initialized only once!
2. require a hard-coded way to get the bean manually
In the test case class we need to get the target bean that needs to be tested from the SPIRNG container through the Ctx.getbean () method, and also perform a styling operation that enforces the type conversion. This tedious operation mist in the code of the test case, which makes people feel cumbersome;
Using the Spring test suite, the properties in the test case class are automatically populated with the corresponding bean for the spring container, without having to manually set the bean!
3. database site vulnerable to damage
Test method changes to the database are persisted to the database. Although it is for the development database, it may affect the subsequent test behavior if the impact of the data operation is persistent. For example, the user inserts a user record with ID 1 in the test method, and the first run will not have a problem, and the second run will cause the test case to fail because of a primary key violation. Therefore, it should be able to complete the functional logic check, but also can restore the scene after the test is completed, will not leave "Sequela";
With the spring test suite, spring will automatically roll back the operation of the database after you validate it, ensuring that the database is not destroyed, so there is no problem with duplicate testing!
4. Inconvenient to check the correctness of data operation
If we insert a successful login log into the log table, we do not check whether a record is actually added to the T_login_log table. In general, we may open the database and visually observe if the corresponding record is inserted, but this is a serious violation of the principle of automated testing. Imagine how to check with the naked eye when testing a program that includes thousands of data manipulation behaviors.
As long as you inherit the use case class of the spring test suite, you can access the database through JdbcTemplate in the same transaction, query the data changes, verify the correctness of the operation!
The following article introduces you to the implementation process (based on the MAVEN project):
Add the following configuration to Maven's pom.xml and omit other spring-related configurations.
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> < dependency> <groupId>org.springframework</groupId> <artifactid>spring-test</ Artifactid> <version>4.2.5.RELEASE</version></dependency>
Define a test base class:
Import Org.junit.runner.RunWith; Import org.springframework.test.context.ContextConfiguration; Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner. Class)//means integrated JUNIT4 for testing @contextconfiguration (locations={"Classpath:spring-config.xml"})//Loading spring configuration file class Basejunit4test {}
Write the specific test class that inherits the test base class that you just tested:
ImportOrg.junit.Test;Import org.springframework.beans.factory.annotation.Autowired; import Com.gk.spring.redis.daoimpl.userdao;import Com.gk.spring.redis.entity.user; Public class springredistest extends Basejunit4test {@Autowired private Userdao Userdao; @Test public void test01 () {User user = new User (); User.setid ("1" ); User.setname ("QQ"
Finally, test it according to your own test requirements.
Spring Test+junit Integrated Use