The spring framework is used in JUnit unit test cases, which I used very directly.
/** * For test case base classes that need to be used with spring * @author http://www.coderli.com*/@RunWith (Springjunit4classrunner. class = {"/spring/applicationcontext.xml" })publicclass Springtest {}
In the course of the test, it was mentioned that you want to get the ApplicationContext instance. As a result, an injection of ApplicationContext is added.
* ** * * * * @blog http://www.coderli.com */@RunWith ( Springjunit4classrunner. class = {"/spring/applicationcontext.xml" })publicclass springtest {@ autowiredprotected applicationcontext ctx;
In fact, Spring has already directly provided a more convenient base class: Abstractjunit4springcontexttests. Modify the code as follows:
/*** For the base class of test cases where spring is required * *@authorLihzh * @alia onecoder * @bloghttp://www.coderli.com */@ContextConfiguration (Locations= {"/spring/applicationcontext.xml" }) Public classSpringtestextendsabstractjunit4springcontexttests { Public<T> T Getbean (class<t>type) {returnApplicationcontext.getbean (type);} PublicObject Getbean (String beanname) {returnApplicationcontext.getbean (beanname);}protectedApplicationContext GetContext () {returnApplicationContext;}}
The code is much more concise.
Now think about the common functions you want, which can be thought of by the general family. Before you do this, you may want to check if there are any tools available:)
Transferred from: http://www.coderli.com/junit-spring-test-applicationcontext/
Springtest Framework junit Unit test Case Get ApplicationContext instance method