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 ();
}