Spring MVC 测试:如何检查模型属性是否不存在?
Spring MVC testing: How do I check if a model attribute does not exist?
我正在使用 Spring 3.2.11.RELEASE。我正在使用新的 3.2 模拟 MVC 框架进行单元测试,通常我可以使用类似以下的方法检查模型属性……
private MockMvc mockMvc;
…
@Test
public final void test()
{
…
mockMvc.perform(get(“/mypath”)
.param("userId", customerId))
.andExpect(status().isOk())
.andExpect(model().attribute(“attr1”, “value”))
.andExpect(view().name(“my view"));
但我如何调整以上内容以验证属性“attr1”未包含在我的模型中?
由于模型属性保存在映射中,您可以检查属性的值是否为 null(假设 null 不是属性的有效值)。
.andExpect(model().attribute("attr1", nullValue());
其中 nullValue()
是 org.hamcrest.Matchers.nullValue
。
比马克的方法更简单:
.andExpect(model().attributeDoesNotExist("attr"))
我正在使用 Spring 3.2.11.RELEASE。我正在使用新的 3.2 模拟 MVC 框架进行单元测试,通常我可以使用类似以下的方法检查模型属性……
private MockMvc mockMvc;
…
@Test
public final void test()
{
…
mockMvc.perform(get(“/mypath”)
.param("userId", customerId))
.andExpect(status().isOk())
.andExpect(model().attribute(“attr1”, “value”))
.andExpect(view().name(“my view"));
但我如何调整以上内容以验证属性“attr1”未包含在我的模型中?
由于模型属性保存在映射中,您可以检查属性的值是否为 null(假设 null 不是属性的有效值)。
.andExpect(model().attribute("attr1", nullValue());
其中 nullValue()
是 org.hamcrest.Matchers.nullValue
。
比马克的方法更简单:
.andExpect(model().attributeDoesNotExist("attr"))