Spring Consolidation JUnit Unit test in Web project development

Source: Internet
Author: User

By doing web development, you find that testing becomes more complicated after object management through spring.

Because all the beans need to be loaded in the Applicationcontext.xml, and then through the @resource to get.

If you want the entire business stream to do almost every time to test, this is very inefficient and cumbersome. It's too tedious to write a test class on your own.

Then I thought spring had a test framework that could consolidate junit for testing.








1. Join a dependent Jar pack

introduce a spring consolidation JUnit jar package. (A JUnit environment is required in the project)

* Spring-test-3.2.0.release.jar

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.


3. Create a test class

Create a test class with the name "tested class name + test" recommended.

: Using annotations to develop:

@RunWith (springjunit4classrunner.class)//fixed wording

@ContextConfiguration ("Classpath:applicationContext.xml")//configuration file path

Test class

public class SpringTest1 {

Injection of Property objects

@Resource (name= "UserService")

Private UserService UserService;

Test method

@Test

public void Demo1 () {

Userservice.save ();

}

}



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.