From 909577082d7e4a74780bf32bf0c54a1370c76b1f Mon Sep 17 00:00:00 2001 From: Deline Neo Date: Sat, 1 Jun 2013 16:11:56 +1000 Subject: [PATCH] Add attributeDoesNotExist ModelResultMatcher Issue: SPR-10509 --- .../web/servlet/result/ModelResultMatchers.java | 17 ++++++++++++++++- .../result/ModelResultMatchersTests.java | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java index 012812f74c..8a34723250 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -87,6 +87,21 @@ public class ModelResultMatchers { }; } + /** + * Assert the given model attributes do not exist + */ + public ResultMatcher attributeDoesNotExist(final String... names) { + return new ResultMatcher() { + @Override + public void match(MvcResult result) throws Exception { + ModelAndView mav = getModelAndView(result); + for (String name : names) { + assertTrue("Model attribute '" + name + "' exists", mav.getModel().get(name) == null); + } + } + }; + } + /** * Assert the given model attribute(s) have errors. */ diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java index ac9ddd2eb2..315095d911 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,7 +70,17 @@ public class ModelResultMatchersTests { this.matchers.attributeExists("bad").match(this.mvcResult); } - @Test + @Test + public void attributeDoesNotExist() throws Exception { + this.matchers.attributeDoesNotExist("bad").match(this.mvcResult); + } + + @Test(expected=AssertionError.class) + public void attributeDoesNotExist_doesExist() throws Exception { + this.matchers.attributeDoesNotExist("good").match(this.mvcResultWithError); + } + + @Test public void attribute_equal() throws Exception { this.matchers.attribute("good", is("good")).match(this.mvcResult); }