Spring Unit Test

Source: Internet
Author: User

Environment Spring + mybatis. The MyBatis uses Automatic Scan mapper Interface Generation Agent injection spring. Spring transactions are implemented using AOP.
1, introducing spring test Jarspring-test-4.1.6.release
2, the Unit test service is as follows:

Packagecom. Order. Service;import org. JUnit. Test;import org. JUnit. Runner. Runwith;import org. Springframework. Beans. Factory. Annotation. Autowired;import org. Springframework. Test. Context. Contextconfiguration;import org. Springframework. Test. Context. JUnit4. Springjunit4ClassRunner;import org. Springframework. Transaction. Annotation. Transactional;Importcom. Jutudi. Common. Result. Gridresult;Importcom. Jutudi. Order. DAO. Pojo. Orderbuyer;Importcom. Jutudi. Order. Service. Orderbuyerservice;@RunWith (Springjunit4classrunner. Class) @ContextConfiguration ("Classpath:spring/applicationcontext*.xml") @Transactionalpublic class Orderbuyerservicetest {@Autowired private orderbuyerservice service;@Test public void Testqueryall () throws Exception {gridresult<orderbuyer> Grid = Service. Querysellorders(New Orderbuyer (),1,Ten);System. out. println("Test"+ Grid. toString());}}

The transaction method is tested because the unit test is rollback by default, so close rollback

@Test    @Transactional    @Rollback(false)    publicvoidtestSaveOrderthrows Exception {        int i = farmOrderService.saveOrder(farmOrder);        100);    }

3, Unit test controller
For details, refer to the official documentation:
http://docs.spring.io/spring/docs/3.2.13.RELEASE/spring-framework-reference/htmlsingle/#spring-mvc-test-framework
http://www.cnblogs.com/naruto469/p/4087821.html a blog after an error

/** * Must be introduced here, otherwise the error status () get () method cannot find */Import static Org. Springframework. Test. Web. servlet. Request. Mockmvcrequestbuilders. Get;/** * Must be introduced here, otherwise the error status () get () method cannot find */Import static Org. Springframework. Test. Web. servlet. Result. Mockmvcresultmatchers. Status;import org. JUnit. Before;import org. JUnit. Test;import org. JUnit. Runner. Runwith;import org. SLF4j. Logger;import org. SLF4j. Loggerfactory;import org. Springframework. Beans. Factory. Annotation. Autowired;import org. Springframework. HTTP. MediaType;import org. Springframework. Test. Context. Contextconfiguration;import org. Springframework. Test. Context. JUnit4. Springjunit4ClassRunner;import org. Springframework. Test. Context. Web. Webappconfiguration;import org. Springframework. Test. Web. servlet. Mockmvc;import org. Springframework. Test. Web. servlet. Mvcresult;import org. Springframework. Test. Web. servlet. Result. Mockmvcresulthandlers;import org. Springframework. Test. Web. servlet. Setup. Mockmvcbuilders;import org. Springframework. Web. Context. Webapplicationcontext;import org. Springframework. Web. servlet. config. Annotation. Enablewebmvc;@RunWith (Springjunit4classrunner. Class)//@ContextConfiguration ("Classpath:spring/applicationcontext*.xml")//@EnableWebMvc//@WebAppConfiguration @runwith (Springjunit4classrunner. Class) @WebAppConfiguration @contextconfiguration ("Classpath:spring/applicationcontext*.xml") @EnableWebMvcpublic class Appfundcontrollertest {@Autowired private webapplicationcontext WAC;Private Mockmvc Mockmvc;Private static final Logger log = Loggerfactory. GetLogger(Appfundcontrollertest. Class);@Before public void Setup () throws Exception {This. Mockmvc= Mockmvcbuilders. Webappcontextsetup(This. WAC). Build();This. Mockmvc= Mockmvcbuilders. Standalonesetup(New Appfundcontroller ()). Build();} @Test public void Testreceive () throws Exception {//1, MOCKMVC. PerformExecute a request;//2, Mockmvcrequestbuilders. Get("/USER/1") constructs a request//3, resultactions. AndexpectAdd an assertion after execution is complete//4, resultactions. AnddoAdd a result handler that indicates what to do with the results, such as using mockmvcresulthandlers here.. Print() outputs the entire response result information. //5, resultactions. AndreturnIndicates that the corresponding result is returned when execution is complete. Mvcresult result =//MOCKMVC. Perform(mockmvcrequestbuilders. Get("/jutudiapp/appfindtopfundclass.html"))        // //. Andexpect(mockmvcresultmatchers. View(). Name("User/view"))        // //. Andexpect(mockmvcresultmatchers. Model(). Attributeexists("User"))        // //. Anddo(mockmvcresulthandlers. Print())        //. Andreturn();Mvcresult result = This. Mockmvc                . Perform(Get ("/jutudiapp/appfindtopfundclass.html"). Accept(mediatype. All). param("id"," the")). Andexpect(Status (). IsOk()). Anddo(mockmvcresulthandlers. Print()). Andreturn();Log. Debug("Query Result:"+ Result. GetResponse(). GetHeader("Content-type"));}}

Spring 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.