Create a new test base class under the Test folder with the content
// Specifies @ContextConfiguration of the configuration file injected by the bean (locations = {"Classpath:application.xml" })// Use the standard JUnit @RunWith annotations to tell JUnit to use the spring Testrunner@RunWith (springjunit4classrunner. Class)publicabstractclassextends abstractjunit4springcontexttests{ protected Logger Logger = Loggerfactory.getlogger ( GetClass ());}
This test class is used for spring-configured projects and needs to be loaded with the XML file, which the test program inherits to test, such as:
Public class extends Springtestcase { @Resource private userservice userservice; = Logger.getlogger (userservicetest. Class); @Test publicvoid selectuserbyidtest () { = Userservice.selectuserbyid (ten); Logger.info ("Find results" + user); } }
Application.xml Example
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/ Aop/spring-aop-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/ Schema/context/spring-context-3.0.xsd "> <!--introducing a JDBC configuration file - <BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Locations"> <List> <value>Classpath:properties/*.properties</value> <!--If you have more than one configuration file, simply add it here - </List> </ Property> </Bean> <!--Configure the data source - <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource"> <!--do not use properties to configure - <!--<property name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "JD Bc:mysql://localhost:3306/learning "/> <property name=" username "value=" root "/> <prop Erty name= "Password" value= "[email protected]"/> - <!--use properties to configure - < Propertyname= "Driverclassname"> <value>${jdbc_driverclassname}</value> </ Property> < Propertyname= "url"> <value>${jdbc_url}</value> </ Property> < Propertyname= "username"> <value>${jdbc_username}</value> </ Property> < Propertyname= "Password"> <value>${jdbc_password}</value> </ Property> </Bean> <!--automatically scan all the xxxxmapper.xml corresponding to the Mapper interface file, so that you do not have a manual configuration Mpper mapping, as long as the Mapper interface class and mapper map file corresponding to it. - <Beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer"> < Propertyname= "Basepackage"value= "Com.lin.dao" /> </Bean> <!--Configuring MyBatis files, mapperlocations configuring **mapper.xml file locations, configlocation configuring Mybatis-config file Locations - <BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean"> < Propertyname= "DataSource"ref= "DataSource" /> < Propertyname= "Mapperlocations"value= "Classpath*:com/lin/mapper/**/*.xml"/> < Propertyname= "Configlocation"value= "Classpath:mybatis/mybatis-config.xml" /> <!--<property name= "typealiasespackage" value= "Com.tiantian.ckeditor.model"/> - </Bean> <!--automatically scan annotations for beans - <Context:component-scanBase-package= "Com.lin.service" /></Beans>
Java Unit Test