Spring test framework JUnit Build test environment does not initialize the spring Bean object through the Web server

Source: Internet
Author: User

Direct use of JUnit to test the shortage of Spring programs
    • you need to manually fetch the bean using hard coding: in the test case, we need to get the target Bean to be tested from the SPIRNG container by means of the Applicationcontext.getbean () method, and also to do the modeling operation.

    • The database site is vulnerable to damage: The test method may make changes to the database records, destroying the database site. While testing is done for the development database, if the impact of the data operation is persistent, there will be a cumulative effect and affect the re-execution of the test case. For example, suppose you insert a t_user record with ID 1 into a database in a test method, there is no problem with the first run, and the second run causes the test case execution to fail because of a primary key violation. Therefore, the test case should be able to complete the test firmware business functions of the correctness of the inspection, but also easy to restore the scene after the test, so that Snow no trace, 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 data operation, it is necessary to access the database through a convenient way in the same transactional environment as the test method to check the performance of test firmware data operation. It is difficult to do this if you are testing directly with JUnit.

The spring test framework, designed for testing based on spring framework applications, makes it easy for test cases to be combined with the spring framework, all of which can be solved.

Writing test Cases

@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"Classpath*:config/spring/applicationcontext.xml"},inheritlocations=false)
@Transactional
public class Test1 {


@Autowired
Public Redistemplate redistemplate;


@Before
public void Beforeaction () {

System.out.println ("before");
}

@Test

@Rollback (True)

public void Testredis () {
Boundlistoperations opt = redistemplate.boundlistops (attributes.linklstkey_elevenselectfive_cq_data);
list<integer> lst = (list<integer>) opt.index (2);
System.out.println (Lst.tostring ());
}
}


Comments:

@RunWith (Springjunit4classrunner.class) indicates that the test case is tested using JUNIT4 and can be replaced by other test frameworks

@ContextConfiguration:

    • Locations: You can specify one or more spring profiles by manually specifying where the spring configuration file is located. As shown below:

      @ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”})

    • Inheritlocations: If you want to inherit the Spring configuration file from the parent test case class, the default is true.

@Rollback (False): Indicates that the test case is rolled back


Note:

When you manipulate the database, the data is not actually inserted into the database.

This is a function of TestContext, the call transaction returns and does not actually insert the database.


Spring test framework JUnit Build test environment does not initialize the spring Bean object through the Web server

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.