Spring-test + JUNIT4 Unit Test

Source: Internet
Author: User

One, using JUnit only for unit testing deficiencies:

1, causing multiple spring container initialization problems

--based on the invocation process of the JUnit test method, each execution of a test method 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;

--Using the Spring test suite, the spring container will only be initialized once!

2, need to use hard-coded method to obtain the bean manually

In the test case class, we need to get the target bean 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 beans of the spring container, without having to manually set the bean

3, the database site vulnerable to damage

Test method The change operation to the database is 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";

Using the Spring test suite, spring will automatically roll back the operations of the database after you verify that the database is not destroyed in the field, so there is no problem repeating the test!

4, inconvenient to check the correctness of data operation

---if we inserted a log into log table for success, we did not check whether a record was 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 spring test suite's use case class, you can access the database through JdbcTemplate in the same transaction, query the data changes, verify the correctness of the operation!

Instance code:

<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.3.5.RELEASE</version>            <scope>test</scope>        </ Dependency>
 PackageCom.sgl.dao;Importjavax.transaction.Transactional;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.test.annotation.Rollback;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner; @Transactional//control transactions, you can also load methods on@RunWith (Springjunit4classrunner.class)//using JUNIT4 for testing@ContextConfiguration ({"/spring-context.xml"})//load a configuration file that can load multiple Public classTestusermapper {Private StaticLogger Logger = Loggerfactory.getlogger (testusermapper.class); @AutowiredPrivateUsermapper Usermapper; @Rollback (Value=false)//do not roll back@Test Public voidDeleteuserbyid () {intCount = Usermapper.deleteuserbyid (2); Logger.info ("Successfully deleted {} bar Data", Count); }}

Spring-test + JUNIT4 Unit Test

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.