Spring MVC parametric test-Junit parameterized

Source: Internet
Author: User

Reference article: Spring MVC full Annotation configuration-no web. xml


The purpose of unit testing, in short, is to test all logic after we add or change some code, especially after we have modified it later (whether adding new features or modifying bugs), we can do the work of re-testing. To reduce the problem that we have solved even before the release of the issue.


Spring MVC testing often seems more complex. In fact his difference is that he needs a servletcontext to simulate our request and response. But spring also provides a mock test interface of request and response for spring MVC, so that our unit test coverage is more than just the Service,dao layer.


We will test JUnit with the configuration of the previous article. In the previous article we used pure Java code to configure spring MVC, so there are three points to note when we write the JUnit test, which is a bit different from the usual JUnit test

    1. The @ContextConfiguration annotations on the test class need to specify the spring configuration file, because we are using a Java configuration, so here we pass our configuration class, remember that is the spring configuration class, Instead of the Web configuration class (note: The configuration of the Web is simulated by us).

    2. Because Controller,component and so on are all using annotations, so our @ContextConfiguration use direct to load. So in his properties, configure a loader. At the same time, because it is web, use annotationconfigwebcontextloader.

    3. Because of the need to simulate servletcontext, we need to add @WebAppConfigurationto our test class.


The code is as follows:

@RunWith (Parameterized.class) @ContextConfiguration (classes = {Webmvcconfiguration.class}, loader = Annotationconfigwebcontextloader.class) @WebAppConfigurationpublic class Controllertest

My goal is to use parameterization to test the controller, so using @runwith is a parameterized runner. Since JUnit's runner can only have one, we have a different way to integrate spring's functionality here.


The next step is to write our test controller with the following code:

@Controllerpublic class TestController {@RequestMapping (value = "/test", method = Requestmethod.get) public @ Responsebody string Test (string name) {return name;}}

In our test class, we are constructing a parameter to the controller's method.


In JUnit, the parameter constructs do not speak much here, @Parameters to tag data sets, private properties and constructors to correspond to our incoming parameters and expected results. So we construct the data in Controllertest:

Parametersprivate string Name;private string Result;public controllertest (string name, string result) {this.name = name ; this.result = result;} @Parameterspublic static collection<string[]> data () {return arrays.aslist (new string[][] {{"Troy", "Troy"},{"Yo Ung "," Young "});}


Before simulating HTTP request response, we need to simulate a request and response environment, but also to let our spring functions into it so that we can use the Spring di and other functions. So both of these are configured before we test:

Private Testcontextmanager testcontextmanager;private Mockmvc mockmvc; @Autowiredprivate webapplicationcontext wac;@ beforepublic void SetUp () throws Exception {Testcontextmanager = new Testcontextmanager (GetClass ()); Testcontextmanager.preparetestinstance (this); MOCKMVC = Mockmvcbuilders.webappcontextsetup (WAC). build ();}

At this point we have the environment, parameters, and then the real test code to write:

@Testpublic void Test () throws Exception {Mvcresult rs = mockmvc.perform (Mockmvcrequestbuilders.get ("/test?name=" + Name ). Andreturn (); Assertequals ("name is not correct", result, new String (Rs.getresponse (). Getcontentasbytearray ()));}


Can be very good to run, has achieved the results we have expected. Later extensions are used only in data to expand the configuration parameters, and then every time we build we can find that our build has a bug.


A lot of specific class use please check it yourself. This article simply explains the simple operation.



This article is from the "Sg-yyz" blog, make sure to keep this source http://sgyyz.blog.51cto.com/5069360/1579991

Spring MVC parametric test-Junit parameterized

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.