View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.test.web.servlet.samples.standalone;
17  
18  import org.junit.Test;
19  
20  import org.springframework.http.MediaType;
21  import org.springframework.stereotype.Controller;
22  import org.springframework.test.web.Person;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.bind.annotation.RequestParam;
25  import org.springframework.web.bind.annotation.ResponseBody;
26  
27  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
28  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
29  import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
30  
31  /**
32   * Tests demonstrating the use of request parameters.
33   *
34   * @author Rossen Stoyanchev
35   */
36  public class RequestParameterTests {
37  
38  	@Test
39  	public void queryParameter() throws Exception {
40  
41  		standaloneSetup(new PersonController()).build()
42  			.perform(get("/search?name=George").accept(MediaType.APPLICATION_JSON))
43  				.andExpect(status().isOk())
44  				.andExpect(content().contentType("application/json;charset=UTF-8"))
45  				.andExpect(jsonPath("$.name").value("George"));
46  	}
47  
48  
49  	@Controller
50  	private class PersonController {
51  
52  		@RequestMapping(value="/search")
53  		@ResponseBody
54  		public Person get(@RequestParam String name) {
55  			Person person = new Person(name);
56  			return person;
57  		}
58  	}
59  
60  }