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