Test Demo for basic Spring test framework

Source: Internet
Author: User
Tags hasproperty

The following is a test instance of the user controller

Import static org.junit.assert.*;import java.util.arraylist;import java.util.list;import  javassist.expr.NewArray;import org.hamcrest.Matchers;import org.junit.Before;import  org.junit.test;import org.junit.runner.runwith;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.result.mockmvcresulthandlers;import  org.springframework.test.web.servlet.setup.mockmvcbuilders;import  Org.springframework.web.context.webapplicationcontext;import com.controller.user.usercontroller;import  com.entity.user.user;import static org.springframework.test.web.servlet.request.mockmvcrequestbuilders.*;import static  org.springframework.test.web.servlet.result.mockmvcresultmatchers.*;import static  org.springframework.test.web.servlet.result.mockmvcresulthandlers.*;import static  org.springframework.test.web.servlet.setup.mockmvcbuilders.*;import static org.hamcrest.matchers.*;@ Runwith (Springjunit4classrunner.class) @ContextConfiguration (locations={"classpath:/spring/ Spring.applicationContext.mysql.xml "}) Public class testusercontroller {private mockmvc  mockMvc;     @Beforepublic  void setup ()  {this.mockMvc =  Standalonesetup (New usercontroller ())        .defaultrequest (Get ("/")        .accept (Mediatype.application_json))         .alwaysexpect (Status (). IsOk ())        .alwaysexpect (content (). ContentType ("ApplicaTion/json;charset=utf-8 "))        .build (); @Testpublic  void testdisplayuser ()  throws exception {this.mockmvc.perform (Get ("/users "). Andexpect (View (). Name (" Userlistview ")). Andexpect (Model (). Attribute (" Users ", Hassize (2)). Andexpect (Model (). Attribute ("Users", Hasitem (Hasproperty ("No",  is ("FF0478")))). Andexpect (Model (). Attribute ("Users", Hasitem ( Hasproperty ("No",  is ("FF0479")))). Anddo (print ());} @Testpublic  void getuser ()  throws exception{this.mockmvc.perform (Get ("/users/5476")). Andexpect (model (). Attribute ("User", Hasproperty ("No",  is ("FF0830"))). Andexpect (Model (). Attribute ("User",  hasproperty ("id",  is (5476))));} @Testpublic  void saveuser ()  throws exception{this.mockmvc.perform (Get ("/user/save"). Param ( "Name",  "Li Yang"). Param ("No",  "FF0833"). Param ("password",  "123"). Param ("DepartmentID",  "101")) . Andexpect (View (). Name ("UserList"));} @Testpublic  VOID&NBsp;updateuser ()  throws exception {this.mockmvc.perform (Get ("/users/update"). PARAM ("id",  " Param ("name",  "Li Yang"). Param ("No",  "FF0834"). Andexpect (View (). Name ("UserList"));}}


Test Demo for basic Spring test framework

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.