Next to my previous article about restassured testing a restful Web service, restassured also has a feature to test your spring MVC Controllers with the RESTASSUREDMOCKMVC unit, This MOCKMVC is based on spring MOCKMVC, which is designed to make it easier for us to use.
Getting Ready
<dependency> <groupId>com.jayway.restassured</groupId> <artifactid>spring-mock- mvc</artifactid> <version>2.4.0</version> <scope>test</scope> </dependen cy> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--O Ptional-<dependency> <groupId>org.hamcrest</groupId> <artifactid>hamcrest- core</artifactid> <version>1.3</version> <scope>test</scope> </dependenc y> <dependency> <groupId>org.hamcrest</groupId> <ARTIFACTID>HAMCREST-LIBRARY&L t;/artifactid> <version>1.3</version> <scope>test</scope> </dependency>< /pre>
Example
Here's the controller we're going to test.
Package Com.wadeshop.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.bind.annotation.ResponseBody; @Controllerpublic class Greetingcontroller { private Static final String template = "Hello,%s!"; @RequestMapping (value = "/greeting", method = Requestmethod.get) @ResponseBody public Greeting Greeting (@ Requestparam (value= "name", Required=false, defaultvalue= "World") String name { return new greeting (String.Format (template, name));} }
The greeting class is as follows
public class Greeting { private final String content; Public String getcontent () { return content; } Public greeting (String content) { this.content = content; } }
# #转载注明出处: http://www.cnblogs.com/wade-xu/p/4311205.html
The next step is to create the spring MVC test class.
Package Com.wadeshop.controller;import static Com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.given; Import static Org.hamcrest.matchers.equalto;import Org.junit.before;import Org.junit.test;import Com.jayway.restassured.module.mockmvc.restassuredmockmvc;public class Greetingcontrollertest { @Before public void configured () { restassuredmockmvc.standalonesetup (new Greetingcontroller ()); } @Test public void Test1 () { given (). Param ("name", "Johan"). When (). Get ("/greeting"). Then (). StatusCode ($). Body ("Content", Equalto ("Hello, johan!")); } @Test public void Test2 () { given (). Param ("name", ""). When (). Get ("/greeting"). Then (). StatusCode ($). Body ("Content", Equalto ("Hello, world!"));} }
The unit test process is nothing more than these steps:
1. Prepare the test environment, the example above is to initialize the MOCKMVC with standalone setup, passing in the controller
2. Incoming parameters construct the request and call
3. Verify the results
Execution results are as follows
Isn't it simple?
This approach is purely unit testing, if you want to emulate the real spring MVC, take the spring MVC complete process, such as dispatcher servlet, type conversion, data binding, etc. You need to use Mockmvcbuilders.webappcontextsetup (Webapplicationcontext). build (); I'll cover it in a future article.
Reference
Https://code.google.com/p/rest-assured/wiki/Usage#Spring_Mock_Mvc_Module
# #转载注明出处: http://www.cnblogs.com/wade-xu/p/4311205.html
It's so simple (continued)! Test Spring MVC Controllers (go) with RESTASSUREDMOCKMVC