JUNIT4 testing the Spring MVC annotation Method

Source: Internet
Author: User

I use the test for JUNIT4

How annotation scanning is used in Spring-servlet.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans         . xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd        Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd ">    <Context:annotation-config/>    <!--automatically scan all annotations for this path -    <Context:component-scanBase-package= "Com.carrefour" />    <!--mainly acting on @controller, activating the mode, the following is a shorthand form, can be manually configured to replace this shorthand form, it will automatically register defaultannotationhandlermapping and Annotationmet Hodhandleradapter, which is required by spring MVC to distribute the request to the @controllers -    <Mvc:annotation-driven/>

The control layer (action layer) uses @controller

The Business Layer (service layer) uses @service,

Persistence layer (DAO layer) using @repository

You can also use the three-tier unified @component is also possible, but recommended to use the above way, easy to distinguish

Detailed introduction also can view link

http://blog.csdn.net/yi3040/article/details/6447289

JUnit Test

 Packagecom.example.thread.test;Importjava.util.List;ImportOrg.apache.xbean.spring.context.FileSystemXmlApplicationContext;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportCom.carrefour.dao.CheckP4AccountDao;ImportCom.carrefour.model.SimpleModel; Public classCheckaccountthtest {PrivateCheckp4accountdao Checkp4accountdao; @Before Public  voidInit ()throwsException {
Loads the spring container, which is an absolute path, with the spring configuration file under Web-inf//ApplicationContext context = new Filesystemxmlapplicationcontext (New string[]{//"E:\\workspaces\\myeclipse 8.6\\carrefour\\webroot\\web-inf\\spring-servlet.xml"// });
Loads the spring container, which is a relative path, and the spring configuration file ApplicationContext context under Web-infNewFilesystemxmlapplicationcontext ("Webroot/web-inf/spring-servlet.xml" ); get the DAO layer implementation class bean, note that because of the annotations used, spring defaults to writing the first letter of the class as the Bean's name
Checkp4accountdao= (Checkp4accountdao) context.getbean ("Checkp4accountdaoimpl"); } @Test Public voidTestall ()throwsexception{
Here you can invoke the DAO layer method List According to the actual situation<SimpleModel> list =checkp4accountdao.getallregion (); System.out.println (List.size ());
System.out.println (finish);
}

There are many test methods, this method of personal feeling more useful and understanding, such as have favorite can be collected

JUNIT4 testing the Spring MVC annotation Method

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.