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  
17  package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
18  
19  import javax.validation.Valid;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import org.springframework.stereotype.Controller;
25  import org.springframework.test.web.Person;
26  import org.springframework.test.web.servlet.MockMvc;
27  import org.springframework.ui.Model;
28  import org.springframework.validation.BindingResult;
29  import org.springframework.web.bind.annotation.RequestMapping;
30  import org.springframework.web.bind.annotation.RequestMethod;
31  
32  import static org.hamcrest.Matchers.*;
33  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
34  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
35  import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
36  
37  /**
38   * Examples of expectations on the content of the model prepared by the controller.
39   *
40   * @author Rossen Stoyanchev
41   */
42  public class ModelAssertionTests {
43  
44  	private MockMvc mockMvc;
45  
46  	@Before
47  	public void setup() {
48  
49  		SampleController controller = new SampleController("a string value", 3, new Person("a name"));
50  
51  		this.mockMvc = standaloneSetup(controller)
52  				.defaultRequest(get("/"))
53  				.alwaysExpect(status().isOk())
54  				.build();
55  	}
56  
57  	@Test
58  	public void testAttributeEqualTo() throws Exception {
59  		mockMvc.perform(get("/"))
60  			.andExpect(model().attribute("integer", 3))
61  			.andExpect(model().attribute("string", "a string value"))
62  			.andExpect(model().attribute("integer", equalTo(3))) // Hamcrest...
63  			.andExpect(model().attribute("string", equalTo("a string value")));
64  	}
65  
66  	@Test
67  	public void testAttributeExists() throws Exception {
68  		mockMvc.perform(get("/"))
69  			.andExpect(model().attributeExists("integer", "string", "person"))
70  			.andExpect(model().attribute("integer", notNullValue()))  // Hamcrest...
71  			.andExpect(model().attribute("INTEGER", nullValue()));
72  	}
73  
74  	@Test
75  	public void testAttributeHamcrestMatchers() throws Exception {
76  		mockMvc.perform(get("/"))
77  			.andExpect(model().attribute("integer", equalTo(3)))
78  			.andExpect(model().attribute("string", allOf(startsWith("a string"), endsWith("value"))))
79  			.andExpect(model().attribute("person", hasProperty("name", equalTo("a name"))));
80  	}
81  
82  	@Test
83  	public void testHasErrors() throws Exception {
84  		mockMvc.perform(post("/persons")).andExpect(model().attributeHasErrors("person"));
85  	}
86  
87  	@Test
88  	public void testHasNoErrors() throws Exception {
89  		mockMvc.perform(get("/")).andExpect(model().hasNoErrors());
90  	}
91  
92  
93  	@Controller
94  	private static class SampleController {
95  
96  		private final Object[] values;
97  
98  		public SampleController(Object... values) {
99  			this.values = values;
100 		}
101 
102 		@RequestMapping("/")
103 		public String handle(Model model) {
104 			for (Object value : this.values) {
105 				model.addAttribute(value);
106 			}
107 			return "view";
108 		}
109 
110 		@RequestMapping(value="/persons", method=RequestMethod.POST)
111 		public String create(@Valid Person person, BindingResult result, Model model) {
112 			return "view";
113 		}
114 	}
115 
116 }