Add attributeDoesNotExist ModelResultMatcher

Issue: SPR-10509
This commit is contained in:
Deline Neo 2013-06-01 16:11:56 +10:00 committed by Rossen Stoyanchev
parent 0a37552beb
commit 98da5a7b2b
2 changed files with 28 additions and 3 deletions

View File

@ -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.
@ -84,6 +84,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.
*/

View File

@ -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);
}