1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.test.web.servlet.result;
18
19 import java.util.Date;
20
21 import org.junit.Before;
22 import org.junit.Test;
23
24 import org.springframework.test.web.servlet.MvcResult;
25 import org.springframework.test.web.servlet.StubMvcResult;
26 import org.springframework.validation.BeanPropertyBindingResult;
27 import org.springframework.validation.BindingResult;
28 import org.springframework.web.servlet.ModelAndView;
29
30 import static org.hamcrest.Matchers.*;
31
32
33
34
35
36
37
38 public class ModelResultMatchersTests {
39
40 private ModelResultMatchers matchers;
41
42 private MvcResult mvcResult;
43 private MvcResult mvcResultWithError;
44
45 @Before
46 public void setUp() throws Exception {
47 this.matchers = new ModelResultMatchers();
48
49 ModelAndView mav = new ModelAndView("view", "good", "good");
50 BindingResult bindingResult = new BeanPropertyBindingResult("good", "good");
51 mav.addObject(BindingResult.MODEL_KEY_PREFIX + "good", bindingResult);
52
53 this.mvcResult = getMvcResult(mav);
54
55 Date date = new Date();
56 BindingResult bindingResultWithError = new BeanPropertyBindingResult(date, "date");
57 bindingResultWithError.rejectValue("time", "error");
58
59 ModelAndView mavWithError = new ModelAndView("view", "good", "good");
60 mavWithError.addObject("date", date);
61 mavWithError.addObject(BindingResult.MODEL_KEY_PREFIX + "date", bindingResultWithError);
62
63 this.mvcResultWithError = getMvcResult(mavWithError);
64 }
65
66 @Test
67 public void attributeExists() throws Exception {
68 this.matchers.attributeExists("good").match(this.mvcResult);
69 }
70
71 @Test(expected=AssertionError.class)
72 public void attributeExists_doesNotExist() throws Exception {
73 this.matchers.attributeExists("bad").match(this.mvcResult);
74 }
75
76 @Test
77 public void attributeDoesNotExist() throws Exception {
78 this.matchers.attributeDoesNotExist("bad").match(this.mvcResult);
79 }
80
81 @Test(expected=AssertionError.class)
82 public void attributeDoesNotExist_doesExist() throws Exception {
83 this.matchers.attributeDoesNotExist("good").match(this.mvcResultWithError);
84 }
85
86 @Test
87 public void attribute_equal() throws Exception {
88 this.matchers.attribute("good", is("good")).match(this.mvcResult);
89 }
90
91 @Test(expected=AssertionError.class)
92 public void attribute_notEqual() throws Exception {
93 this.matchers.attribute("good", is("bad")).match(this.mvcResult);
94 }
95
96 @Test
97 public void hasNoErrors() throws Exception {
98 this.matchers.hasNoErrors().match(this.mvcResult);
99 }
100
101 @Test(expected=AssertionError.class)
102 public void hasNoErrors_withErrors() throws Exception {
103 this.matchers.hasNoErrors().match(this.mvcResultWithError);
104 }
105
106 @Test
107 public void attributeHasErrors() throws Exception {
108 this.matchers.attributeHasErrors("date").match(this.mvcResultWithError);
109 }
110
111 @Test(expected=AssertionError.class)
112 public void attributeHasErrors_withoutErrors() throws Exception {
113 this.matchers.attributeHasErrors("good").match(this.mvcResultWithError);
114 }
115
116 @Test
117 public void attributeHasNoErrors() throws Exception {
118 this.matchers.attributeHasNoErrors("good").match(this.mvcResult);
119 }
120
121 @Test(expected=AssertionError.class)
122 public void attributeHasNoErrors_withoutAttribute() throws Exception {
123 this.matchers.attributeHasNoErrors("missing").match(this.mvcResultWithError);
124 }
125
126 @Test(expected=AssertionError.class)
127 public void attributeHasNoErrors_withErrors() throws Exception {
128 this.matchers.attributeHasNoErrors("date").match(this.mvcResultWithError);
129 }
130
131 @Test
132 public void attributeHasFieldErrors() throws Exception {
133 this.matchers.attributeHasFieldErrors("date", "time").match(this.mvcResultWithError);
134 }
135
136 @Test(expected=AssertionError.class)
137 public void attributeHasFieldErrors_withoutAttribute() throws Exception {
138 this.matchers.attributeHasFieldErrors("missing", "bad").match(this.mvcResult);
139 }
140
141 @Test(expected=AssertionError.class)
142 public void attributeHasFieldErrors_withoutErrorsForAttribute() throws Exception {
143 this.matchers.attributeHasFieldErrors("date", "time").match(this.mvcResult);
144 }
145
146 @Test(expected=AssertionError.class)
147 public void attributeHasFieldErrors_withoutErrorsForField() throws Exception {
148 this.matchers.attributeHasFieldErrors("date", "good", "time").match(this.mvcResultWithError);
149 }
150
151 @Test
152 public void attributeHasFieldErrorCode() throws Exception {
153 this.matchers.attributeHasFieldErrorCode("date", "time", "error").match(this.mvcResultWithError);
154 }
155
156 @Test(expected = AssertionError.class)
157 public void attributeHasFieldErrorCode_withoutErrorOnField() throws Exception {
158 this.matchers.attributeHasFieldErrorCode("date", "time", "incorrectError").match(this.mvcResultWithError);
159 }
160
161 @Test
162 public void attributeHasFieldErrorCode_startsWith() throws Exception {
163 this.matchers.attributeHasFieldErrorCode("date", "time", startsWith("err")).match(this.mvcResultWithError);
164 }
165
166 @Test(expected = AssertionError.class)
167 public void attributeHasFieldErrorCode_startsWith_withoutErrorOnField() throws Exception {
168 this.matchers.attributeHasFieldErrorCode("date", "time", startsWith("inc")).match(this.mvcResultWithError);
169 }
170
171 private MvcResult getMvcResult(ModelAndView modelAndView) {
172 return new StubMvcResult(null, null, null, null, modelAndView, null, null);
173 }
174 }