Spring Test+junit Perfect Combination

Source: Internet
Author: User
Tags java web
With the principle that "programmers who don't write unit tests are not good programmers," I'm sticking to the unit test, not saying that all Java Web Apps are based on spring, but at least more than half are based on spring. It is found that after bean management through spring, there are a variety of deficiencies in testing,
For example, when a lot of people do unit tests, they also initialize the spring container in the Before method, causing the container to be initialized multiple times. [Java]View Plain copy print? @Before public void init () {ApplicationContext ctx = new Filesystemxmlapplicationcontext ("Classpath:spring/sp         Ring-basic.xml ");         Basedao = (Ibasedao) ctx.getbean ("Basedao");    Assertnotnull (Basedao); }

When developing a spring based application, if you also use JUnit directly for unit testing, you miss the most important hard dish in spring feast.
Before we go into this dish, let's talk about what's the problem with using JUnit directly for unit testing in a spring-based Javaweb project
1 causes multiple spring container initialization issues

Each execution of a test method creates an instance of a test case and invokes the Setup () method, depending on the calling process of the JUnit test method. As a general result, we initialize the spring container in the Setup () method, which means that if the test case has a number of test methods, the spring container will be repeatedly initialized multiple times. Although the speed of initializing a spring container is not too slow, the impact on test performance cannot be overlooked, because 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 duplicated each time a test method is executed;

With the spring test suite, the spring container is initialized only once.

2 need to use hard code to get the bean manually

In the test case class we need to get the target bean from the SPIRNG container to test from the Ctx.getbean () method, and also to perform a modeling operation that casts the type conversion. This tedious operation is in the code of the test case, which makes people feel cumbersome;

Using the Spring test suite, properties in the test case class are automatically populated with the corresponding bean for the spring container
Without having to set up the bean manually.

3 The database site is vulnerable to damage

The test method changes to the database are persisted to the database. Although the operation is for the development database, if the impact of the data operation is persistent, it may affect the subsequent test behavior. For example, users insert a user record with ID 1 in the test method, the first run will not be a problem, and the second run will cause the test case to fail because of the primary key conflict. Therefore, it should be able to complete the functional logic check, but also to restore the site after the test is completed, will not leave "Sequela";

With the spring test suite, Spring automatically rolls back the operation of the database after you validate it, ensuring that the database is not compromised, so repeated testing does not cause problems.

4) Inconvenient to check the correctness of the data operation

If we insert a successful login log into the login log, we do not check whether a record is actually added to the T_login_log table. In general, we may be opening the database, the naked eye to see whether the corresponding record, but this is a serious violation of the principle of automated testing. Imagine how to test with the naked eye when testing programs that include thousands of data manipulation behaviors.

As long as you inherit the use case class of the spring test suite, you can access the database in the same transaction, query the data changes, and verify the correctness of the operation by JdbcTemplate.

After reading the above, I believe that you already know what I say the hard dish is.
Let's take a look at how the code has become elegant after using the Spring test suite. 1. Join a dependency pack

The test framework for spring requires the inclusion of the following dependencies: JUnit 4 Spring Test (Test package in spring Framework) Spring related other dependencies (no more details, that is, context, etc.)
If you use MAVEN, add the following dependencies in a spring-based project:[HTML]  View plain  copy  print? <dependency>               < groupid>junit</groupid>                <artifactId>junit</artifactId>                <version>4.9</version>                <scope>test</scope>            </dependency>    <dependency>                <groupId>org.springframework</groupId>               <artifactid>spring-test</ artifactid>               <version> 3.2.4.release  </version>                <scope>provided</scope>            </dependency>   

2. Create test source directories and Packages

Here, it is recommended to create a source file directory with SRC peers because the classes within SRC are prepared for future products, and the classes here are for testing purposes only. The name of the package can be the same as the directory in SRC, so there is no conflict in the test source directory, and the name is exactly the same, more convenient to retrieve. This is also the MAVEN agreement.
3. Create test class 1 base class, is actually used to load the configuration file
[Java] view plain copy print?

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.