Java Unit Test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.