Using the Spring 2.5 testcontext test framework

Source: Internet
Author: User


The most important features that spring 2.5 can add to Spring 2.0 boil down to the following 3 points:

The IoC function based on annotations;

The Spring MVC function based on annotation drive;

A TestContext test framework based on annotations.

Spring recommends that developers use the new TestContext test framework based on annotations, which we will cover in detail.

The Spring test box architecture provided by the lower version of spring is extended on a JUnit 3.8 basis and provides several test base classes. The new TestContext test framework and the lower version of the test framework added by Spring 2.5 are not related. It uses a new annotation technology to make POJO a test case for Spring, and TestContext has added new features, in addition to all the features of the old test framework, TestContext can run on JUnit 3.8, JUnit 4.4, TestNG and other testing frameworks.

Disadvantages of using JUnit to test Spring programs directly

In flops, a master of application development in spring 2.x-, the author has pointed out that if you use the JUnit test program based on spring, there are 4 obvious deficiencies:

Causes the Spring container to initialize multiple times: Depending on the calling process of the JUnit test case, each execution of a test method will re-create a test case instance and invoke its SetUp () method. Because in general, we initialize the spring container in the SetUp () method, which means how many test methods are in the test case, and how many times the spring container is initialized repeatedly.

You need to manually get the bean in a hard-coded way: In a test case, we need to get the target Bean from the SPIRNG container to test from the Applicationcontext.getbean () method, and also to sculpt.

Database site is vulnerable: test methods may make changes to database records, destroying the database site. While testing is done for the development database, if the impact of the data manipulation is persistent, it will create a cumulative effect and affect the execution of the test case. For example, suppose you insert a t_user record with ID 1 to the database in a test method, the first run will not be a problem, and the second run will cause the test case execution to fail because of the primary key conflict. So the test case should not only be able to complete the test firmware business function correctness check, and can easily be completed after the test to restore the scene, so that no trace in the snow, geese have no trace.

It is not easy to access the database under the same transaction to verify the correctness of the business operation: when testing the firmware operation database, in order to detect the correctness of the data operation, it is necessary to access the database in the same transaction environment as the test method to check the execution effect of the test firmware data operation. It's hard to do this if you're testing directly with JUnit.

The spring test framework, designed specifically to test the Spring Framework application, allows test cases to be easily combined with the spring framework, all of which will be solved.

A Spring service class that needs testing

Before using the Textcontext test framework specifically, let's take a look at the UserService service classes that need to be tested. The UserService service class has a service method that handles user logins, and the code looks like this:

Listing 1. service classes that need testing

package com.baobaotao.service;

import com.baobaotao.domain.LoginLog;
import com.baobaotao.domain.User;
import com.baobaotao.dao.UserDao;
import com.baobaotao.dao.LoginLogDao;

public class UserService{

   private UserDao userDao;
   private LoginLogDao loginLogDao;

   public void handleUserLogin(User user) {
     user.setCredits( 5 + user.getCredits());
     LoginLog loginLog = new LoginLog();

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: 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.