Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 91 lines (59 sloc) 5.082 kB
18de87d @rstoyanchev readme
rstoyanchev authored
1 Spring MVC Test Support
016668c @rstoyanchev Rename MockMvcServer to MockMvc and *ServerBuilders to *MockMvcBuilders
rstoyanchev authored
2 =======================
18de87d @rstoyanchev readme
rstoyanchev authored
3
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
4 The goal of this project is to facilitate testing _Spring MVC_ controllers on the server side and _RestTemplate_ based code on the client side.
18de87d @rstoyanchev readme
rstoyanchev authored
5
aae27e8 @rstoyanchev Update readme
rstoyanchev authored
6 This code will be included in the `spring-test` module of the __Spring Framework__. Its present home here allows us to evolve it on a flexible release schedule and with community feedback potentially accommodating a wide range of scenarios.
18de87d @rstoyanchev readme
rstoyanchev authored
7
016668c @rstoyanchev Rename MockMvcServer to MockMvc and *ServerBuilders to *MockMvcBuilders
rstoyanchev authored
8 Server-Side
9 ===========
18de87d @rstoyanchev readme
rstoyanchev authored
10
016668c @rstoyanchev Rename MockMvcServer to MockMvc and *ServerBuilders to *MockMvcBuilders
rstoyanchev authored
11 Overview
12 --------
9bb805d @rstoyanchev readme update
rstoyanchev authored
13 Annotated-controllers depend on Spring MVC to handle many things such as mapping requests, performing data binding and validation, setting the response status, writing to the body of the response using the correct content type, and many more.
18de87d @rstoyanchev readme
rstoyanchev authored
14
aae27e8 @rstoyanchev Update readme
rstoyanchev authored
15 To test all that you may instantiate an in-memory Servlet container driving requests with _JWebUnit_ or you may use a test tool such as _JMeter_ or _Selenium_. These options are all valid. However they take longer to execute and can only perform black-box testing.
18de87d @rstoyanchev readme
rstoyanchev authored
16
aae27e8 @rstoyanchev Update readme
rstoyanchev authored
17 The aim of this project is to make it easy to test controllers by building on the familiar `MockHttpServletRequest` and the `MockHttpServletResponse` from the `spring-test` module and without the need for a Servlet container. Whether you want to point to one controller or to test with your complete web application context setup, it should be easy to send a request and verify the results.
18de87d @rstoyanchev readme
rstoyanchev authored
18
016668c @rstoyanchev Rename MockMvcServer to MockMvc and *ServerBuilders to *MockMvcBuilders
rstoyanchev authored
19 Examples
20 --------
18de87d @rstoyanchev readme
rstoyanchev authored
21
9bb805d @rstoyanchev readme update
rstoyanchev authored
22 Test an `@ResponseBody` method in a controller:
18de87d @rstoyanchev readme
rstoyanchev authored
23
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
24 MockMvcBuilders.standaloneSetup(new TestController()).build()
0163790 @rstoyanchev Update readme
rstoyanchev authored
25 .perform(get("/form"))
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
26 .andExpect(status().isOk())
27 .andExpect(content().type("text/plain"))
28 .andExpect(content().string("hello world"));
18de87d @rstoyanchev readme
rstoyanchev authored
29
9bb805d @rstoyanchev readme update
rstoyanchev authored
30 Test binding failure by pointing to Spring MVC XML-based context configuration:
18de87d @rstoyanchev readme
rstoyanchev authored
31
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
32 MockMvcBuilders.xmlConfigSetup("classpath:org/examples/servlet-context.xml").build()
0163790 @rstoyanchev Update readme
rstoyanchev authored
33 .perform(get("/form"))
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
34 .andExpect(status().isOk())
35 .andExpect(model().attributeHasErrors("formBean"))
533783c @hlascelles Updated examples
hlascelles authored
36 .andExpect(view().name("form"));
18de87d @rstoyanchev readme
rstoyanchev authored
37
9bb805d @rstoyanchev readme update
rstoyanchev authored
38 Test serving a resource by pointing to Spring MVC Java-based application configuration:
18de87d @rstoyanchev readme
rstoyanchev authored
39
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
40 MockMvcBuilders.annotationConfigSetup(TestConfiguration.class).build()
0163790 @rstoyanchev Update readme
rstoyanchev authored
41 .perform(get("/resources/Spring.js"))
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
42 .andExpect(content().type("application/octet-stream"))
43 .andExpect(content().string(containsString("Spring={};")));
18de87d @rstoyanchev readme
rstoyanchev authored
44
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
45 The last example uses a Hamcrest matcher to check if the content contains specific text.
46
47 Tips on Getting Started
48 -----------------------
49
4e93db3 @rstoyanchev Add link to presentation in README
rstoyanchev authored
50 See this [presentation](http://rstoyanchev.github.com/spring-31-and-mvc-test/#97).
51
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
52 There are many more examples in the [org.springframework.test.web.server.samples](spring-test-mvc/tree/master/src/test/java/org/springframework/test/web/server/samples) package.
53
54 The API is designed to be fluent and readable. Therefore to learn we recommend writing some tests and using code completion to discover what is available.
55
56 Eclipse developers should add the following classes as "Favorites" under Preferences/Java/Editor/Content Assist:
57 _MockMvcBuilders.*_, _MockMvcRequestBuilders.*_, _MockMvcResultMatchers.*_, and _MockMvcResultHandlers.*_.
58
59 Now when you use _Ctrl+Space_, Eclipse will suggest matching static factory methods from those classes.
18de87d @rstoyanchev readme
rstoyanchev authored
60
d31356f @rstoyanchev readme update
rstoyanchev authored
61 Limitations
62 -----------
18de87d @rstoyanchev readme
rstoyanchev authored
63
731ce1c @rstoyanchev Review and refactor API for expectation setting with ResultMatchers.
rstoyanchev authored
64 Most rendering technologies should work as expected. For _Tiles_ and _JSP_, while you can test with your existing configuration as is, no actual JSP-based rendering will take place. Instead you can verify the path the request was forwarded to (i.e. the path to the JSP page) or you can also verify the selected view name.
18de87d @rstoyanchev readme
rstoyanchev authored
65
0800f32 @irfn Added Maven snapshot repository url
irfn authored
66 Maven
67 =====
68 You can get it from the Spring Maven Snapshot repository:
69 http://maven.springframework.org/snapshot
70
71 <dependency>
72 <groupId>org.springframework</groupId>
73 <artifactId>spring-test-mvc</artifactId>
74 <version>1.0.0.BUILD-SNAPSHOT</version>
75 </dependency
76
18de87d @rstoyanchev readme
rstoyanchev authored
77 Contributions
016668c @rstoyanchev Rename MockMvcServer to MockMvc and *ServerBuilders to *MockMvcBuilders
rstoyanchev authored
78 =============
18de87d @rstoyanchev readme
rstoyanchev authored
79
80 If you see anything you'd like to change we encourage taking advantage of github's social coding features by making the change in a [fork of this repository](http://help.github.com/forking/) and sending a pull request.
81
9bb805d @rstoyanchev readme update
rstoyanchev authored
82 To report an issue the Spring Framework forum or the Spring JIRA creating requests under the component _"SpringTEST"_.
83
18de87d @rstoyanchev readme
rstoyanchev authored
84 Before we accept a non-trivial patch or pull request we will need you to sign the [contributor's agreement] (https://support.springsource.com/spring_committer_signup). Signing the contributor's agreement does not grant anyone commit rights to the main repository, but it does mean that we can accept your contributions, and you will get an author credit if we do. Active contributors might be asked to join the core team, and given the ability to merge pull requests.
85
ed221f9 @rstoyanchev Rename MockMvc to MockWebMvc
rstoyanchev authored
86 Acknowledgements
87 ================
88
89 This project draws inspiration from similar [server-side](http://static.springsource.org/spring-ws/sites/2.0/reference/html/server.html#d4e1487) and [client-side](http://static.springsource.org/spring-ws/sites/2.0/reference/html/client.html#d4e1860) test support introduced in Spring Web Services 2.0.
18de87d @rstoyanchev readme
rstoyanchev authored
90
Something went wrong with that request. Please try again.