Skip to content

Commit 9095770

Browse files
delinerstoyanchev
authored andcommitted
Add attributeDoesNotExist ModelResultMatcher
Issue: SPR-10509
1 parent 4c991ba commit 9095770

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,6 +87,21 @@ public void match(MvcResult result) throws Exception {
8787
};
8888
}
8989

90+
/**
91+
* Assert the given model attributes do not exist
92+
*/
93+
public ResultMatcher attributeDoesNotExist(final String... names) {
94+
return new ResultMatcher() {
95+
@Override
96+
public void match(MvcResult result) throws Exception {
97+
ModelAndView mav = getModelAndView(result);
98+
for (String name : names) {
99+
assertTrue("Model attribute '" + name + "' exists", mav.getModel().get(name) == null);
100+
}
101+
}
102+
};
103+
}
104+
90105
/**
91106
* Assert the given model attribute(s) have errors.
92107
*/

spring-test-mvc/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,7 +70,17 @@ public void attributeExists_doesNotExist() throws Exception {
7070
this.matchers.attributeExists("bad").match(this.mvcResult);
7171
}
7272

73-
@Test
73+
@Test
74+
public void attributeDoesNotExist() throws Exception {
75+
this.matchers.attributeDoesNotExist("bad").match(this.mvcResult);
76+
}
77+
78+
@Test(expected=AssertionError.class)
79+
public void attributeDoesNotExist_doesExist() throws Exception {
80+
this.matchers.attributeDoesNotExist("good").match(this.mvcResultWithError);
81+
}
82+
83+
@Test
7484
public void attribute_equal() throws Exception {
7585
this.matchers.attribute("good", is("good")).match(this.mvcResult);
7686
}

0 commit comments

Comments
 (0)