Today, I reviewed the reference manual for spring2.0 for countless people-Chapter 1 Testing. concentration is the essence of the updated spring wiki-spring unit test points. |
Note: The document is still being modified. You are advised to check the latest wiki version.
1. tests requiring spring dependency Injection
To test the bean managed by spring, you can construct beanfactory by yourself, inherit from abstractdependencyinjectionspringcontexttests, implement the Public String [] getconfiglocations () function, and return the array of applicationcontext file paths.
protectedString[] getConfigLocations() { returnnewString[]{"classpath*:spring/*.xml", "classpath*:spring/test/*.xml"}; }
And explicitly write some setter functions of the variables to be injected.
Tips1: this base class has an applicationcontext member variable. Therefore, in addition to setter injection, you can use applicationcontext. getbean () to retrieve the desired bean at any time.
Tips2: note that the base class is autowire by type by default. Therefore, if there are two beans of the same type in the context file, an error is returned. It may need to be in the getconfiglocations () function, setautowiremode (autowire_by_name); set it back to the by name, or cancel the setter function and use applicationcontext. getbean () to explicitly search for beans.
2. Dao Test
Abstracttransactionaldatasourcespringcontexttests inherits from abstractdependencyinjectionspringcontexttests. In addition to being able to perform classes, it also manages each test transaction and rolls back all operations after each test by default.
Deep explanation: the implementation of this class actually depends onPlatformtransactionmanager. Because autowrie by type is used, you can name it as needed.
It depends onDatasource.
Tips1: If you need to submit after the test, you need to setrollback (false); or call setcomplete ()
Tips2: this base class also creates a jdbctemplate variable through the injected datasource. You can run SQL to check the hibernate results. Spring will ensure that the query is executed in the same transaction. To work properly, you need to tell your ORM tool 'refree' its changed content, such as using hibernateSessionInterfaceFlush ()Method.
Tips3: In addition to tips2, there are three simple functions: countrowsintable (string tablename), deletefromtables (string [] names), and executesqlscript (string sqlresourcepath, Boolean continueonerror.
3. Controller Test
Controller testing is generally to use mockobject separation service layer, to copy the WEB-INF/under the relevant file copy to classpath, and controller does not contain too much logic, all the test controller is a bit thankless, we recommend that you directly use selenium for integration testing. For more information, see (selenium test Overview ).
4. springside Test
Because the default base class name of spring is long, springside is in the org. springside. core. test re-inherits them and provides the getconfiglocations () function to read all context files stored according to the springside file storage rules by default.
By default, the getconfiglocations () function that reads all context files has an impact on the speed and test isolation, and can be implemented in sub-classes. However, it is also annoying to re-write the relevant context files one by one. In fact, the speed is acceptable even in the case of full lazy-load. Balance your choice.
For crud tests, the variable names in the helloworld example are generalized and can be quickly copied to another test.
In addition, pay attention to the files under resources/spring/test, and use spring's propertyoverrideconfigurer to set the attributes of each bean in applicatoncontext during the test, such as specifying the datasource for testing, for detailed usage, see spring configuration points.