SPRINGMVC Controller JUnit Test

Source: Internet
Author: User

Directory

    • 1 establishing the Usercontroller class
    • 2 Building a test class
    • 3 JUnit Test
    • 4 several JAR packages

Build the SSM framework for the first time, integrate SPRINGMVC to complete controller testing, find information and solve problems.

Is my full directory:

Back to top 1 build Usercontroller class

Code Listing 1-1:usercontroller.java

Package Org.sei.controller;Import Javax.annotation.Resource;Import Javax.servlet.http.HttpServletRequest;Import Org.springframework.stereotype.Controller;Import Org.springframework.ui.Model;Import org.springframework.web.bind.annotation.RequestMapping;Import Org.sei.pojo.User;Import Org.sei.service.UserService;@Controller@RequestMapping ("/user")PublicClassusercontroller{@ResourcePrivate UserService UserService;@RequestMapping ("/showuser1")Public StringToindex(HttpServletRequest Request,model Model) {int userId = Integer.parseint (Request.getparameter ("id")); User user = this.userService.getUserById (userId); Model.addattribute ("user", user); return User.getusername ();} @RequestMapping ("/showuser3") public String toIndex1(User u,model Model) //Note: Different parameters {  int userId = U.getid (); User user = this.userService.getUserById (userId); Model.addattribute ("user", user); return User.getusername ();} }
Back to top 2 build test class

Code Listing 2-1:mocktestcontroller.java

Package org.sei.testmybatis;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.setup.MockMvcBuilders;Import Org.springframework.web.context.WebApplicationContext;ImportStatic org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;ImportStatic org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;ImportStatic org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@RunWith (Springjunit4classrunner.class)@ContextConfiguration (locations={"Classpath:spring-mybatis.xml","Classpath:spring-mvc.xml"})@WebAppConfigurationPublicClassmocktestcontroller{protected MOCKMVC Mockmvc;@Autowiredprotected Webapplicationcontext WAC;@Before ()This method executes all the way before each method executes.PublicvoidSetup() {MOCKMVC = Mockmvcbuilders.webappcontextsetup (WAC). build ();Initialize the Mockmvc object}@TestPublicvoidgetallcategorytest () throws Exception {String responsestring = Mockmvc.perform (Get ( "/user/showuser1") //The requested URL, the requested method is get //get ("/user/showuser2 ")//requested URL, the requested method is get. ContentType (mediatype.application_form_urlencoded) //the format of the data. Param (  "id",  "1") //add parameter (can add multiple) //.param ("id", "3")//Add parameter (multiple can be added). Andexpect (Status (). IsOk ()) // The returned status is Anddo (print ()) //print out the request and the corresponding content. Andreturn (). GetResponse (). Getcontentasstring ( ); //converts the corresponding data to String System.out.println ( "-----returned JSON =" + responsestring); }}
Back to top 3 JUnit test
    • The database tables tested are as follows:

    • Test 1: Parameters (HttpServletRequest Request,model Model)

    • Test 2: Parameters (User U,model Model)

Back to top 4 several jar packages

Many problems come from the fact that there are few jar packages.

Here are the Maven dependencies for several jar packages:

Code Listing 4-1:pom.xml (partial)

        <Dependency><Groupid>javax.servlet</Groupid><Artifactid>javax.servlet-api</Artifactid><version>3.1.0</Version></Dependency><!--into JSON--<Dependency><Groupid>org.codehaus.jackson</Groupid><Artifactid>jackson-mapper-asl</Artifactid><version>1.9.13</Version></Dependency><Dependency><Groupid>com.fasterxml.jackson.core</Groupid><Artifactid>jackson-core</Artifactid><version>2.8.3</Version></Dependency><Dependency><Groupid>com.fasterxml.jackson.core</Groupid><Artifactid>jackson-databind</artifactid> <version>2.8.3< Span class= "Hljs-tag" ></version> </ dependency> <dependency> <groupid>com.fasterxml.jackson.core< /groupid> <artifactId> Jackson-annotations</artifactid>  <version>2.8.3</version> Span class= "Hljs-tag" ></DEPENDENCY>        

Source: https://www.cnblogs.com/oucbl/p/5943743.html

SPRINGMVC Controller JUnit 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.