Spring (v) Integration of JUnit Unit tests----Spring Framework

Source: Internet
Author: User

1. To simplify JUnit testing, the spring framework can also be used to integrate testing
2. Specific steps
Requirement: There must be a junit environment first (that is, a development environment that has been imported into the JUNIT4)
Step one: Introduce Spring-test.jar in the program
Step Two: Add annotations on the specific test class

@RunWith (Springjunit4classrunner.class)  
@ContextConfiguration ("Classpath:applicationContext.xml")  
public class SpringDemo1 {  
  
    @Resource (name = "UserService")  
    private userservice userservice;  
  
    @Test public  
    void Demo2 () {  
        userservice.save ();  
    }  
Explanation for the following annotations:
@RunWith: Used to specify the JUnit runtime environment, which is provided by JUnit to other framework test environment Interface Extensions, in order to facilitate the use of spring's dependency injection, Spring provides org.springframework.test.context.junit4.SpringJUnit4ClassRunner as the JUnit test environment
@ContextConfiguration ({"Classpath:applicationContext.xml", "classpath:spring/buyer/ Applicationcontext-service.xml "})
Import the configuration file, where my ApplicationContext profile is categorized according to the module. Multiple "applicationcontext-service.xml" files are introduced if more than one module is available. If all is written in "Applicationcontext.xml" then import this way:
@ContextConfiguration (locations = "Classpath:applicationContext.xml")
We can also create a base class
@RunWith (Springjunit4classrunner.class)//Use JUNIT4 to test  
@ContextConfiguration ("Classpath: Applicationcontext.xml ")//load config file public  
class Basejunit4test {  
}  
Then, the specific test class inherits the base class.
public class Demo extends basejunit4test{  
  
    @Resource (name= "Userserviceimpl")  
    private userservice userservice;  
  
    @Test public  
    void Run1 () {  
        Userservice.sayhello ();  
    }  

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.