在spring mvc app中,我提交id并使用格式化程序将该id转换为对象。它在容器中运行良好。
但在单元测试环境中,我发现了一个问题。
我嘲笑格式化程序总是返回我的测试值,这很好,它被注入到ModelAttribute中。但是在BindingResult中,对result.getFieldValue("location")
的调用例如返回null,但仅在MockMvc上下文中。
这是测试用例:
/**
* Tests the inventory update for existing inventory records.
* @throws Exception
*/
@Test
public void testUpdateExistingProductInventory() throws Exception{
logger.entry();
VariantInventory oldInventory = new VariantInventory();
oldInventory.setId(20l);
Product product = ProductBuilder.buildBasicExisting();
Location location = new Location();
location.setId(3l);
ProductVariant variant = new ProductVariant();
variant.setId(2l);
// check the formatter is working
Mockito.when(mockProductFormatter.parse(((String)Mockito.anyObject()), ((Locale)Mockito.anyObject()))).thenReturn(product);
Product p = mockProductFormatter.parse("1", null);
Assert.assertEquals(p, product);
// check the formatter is working
Mockito.when(mockLocationFormatter.parse(((String)Mockito.anyObject()), ((Locale)Mockito.anyObject()))).thenReturn(location);
Location l = mockLocationFormatter.parse("3", null);
Assert.assertEquals(l, location);
// check the formatter is working
Mockito.when(mockVariantFormatter.parse(((String)Mockito.anyObject()), ((Locale)Mockito.anyObject()))).thenReturn(variant);
ProductVariant pv = mockVariantFormatter.parse("2", null);
Assert.assertEquals(pv, variant);
// check the formatter is working
Mockito.when(mockInventoryFormatter.parse(((String)Mockito.anyObject()), ((Locale)Mockito.anyObject()))).thenReturn(oldInventory);
VariantInventory v = mockInventoryFormatter.parse("20", null);
Assert.assertEquals(v, oldInventory);
this.mockMvc.perform(MockMvcRequestBuilders.post("/ajax/products/update/inventory")
.param("product", "1")
.param("variant", "2")
.param("location", "3")
.param("status", "ACTIVE")
.param("quantityOnHand", "30.5")
.param("lowStockQuantity", "10")
.param("inventory", "20")
)
.andExpect(status().isOk());
Mockito.verify(mockInventoryService, Mockito.times(1)).updateExisting(Mockito.eq(oldInventory), Mockito.any(VariantInventory.class));
logger.exit();
}
这是控制器的相对部分:
@RequestMapping(value = "/ajax/products/update/inventory", method= RequestMethod.POST)
public @ResponseBody
AJAXResponse updateProductInventory(@ModelAttribute ProductInventoryFormWrapper formWrapper, BindingResult result,
ModelMap map) {
logger.entry();
logger.debug("Getting product data");
if (!result.hasErrors()) {
inventoryValidator.validate(formWrapper, result);
}
}
然后跳过一些项目,这是失败的相关验证,我将location
作为字段传递。
ValidationUtils.rejectIfEmptyOrWhitespace(errors, field, "required.field", new String[]{label});
由于必然存在错误,该对象无法验证。
如果调试控制器,我观察到的是:
因此,由于某种原因,绑定结果没有注册格式化字段或其他内容。请注意,这只发生在单元测试中,而不是在容器中。
有谁知道如何解决?
我已经完成了一些调试,并且在AbstractPropertyBindingResult
的代码块中失败了。 value
可以正常运行,直到调用conversionService进行转换。我还没有下载超出该方法的来源,所以我无法确切地知道它失败的原因,但在convert
方法的某个地方,它已从正确的方向转变value,为null。我推测是因为我使用的是MockObjects,也许它会调用一些我无法预料会返回价值的东西。
@Override
protected Object formatFieldValue(String field, Object value) {
String fixedField = fixedField(field);
// Try custom editor...
PropertyEditor customEditor = getCustomEditor(fixedField);
if (customEditor != null) {
customEditor.setValue(value);
String textValue = customEditor.getAsText();
// If the PropertyEditor returned null, there is no appropriate
// text representation for this value: only use it if non-null.
if (textValue != null) {
return textValue;
}
}
if (this.conversionService != null) {
// Try custom converter...
TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
return this.conversionService.convert(value, fieldDesc, strDesc);
}
}
return value;
}
答案 0 :(得分:0)
好的,这是一个艰难的,所以我真的不希望任何人回答。但这就是答案。我是对的,Mock在验证中被调用了。所以我不得不为格式化程序添加一个额外的模拟方法(print):
// check the formatter is working
Mockito.when(mockInventoryFormatter.parse(((String)Mockito.anyObject()), ((Locale)Mockito.anyObject()))).thenReturn(oldInventory);
// this was added
Mockito.when(mockInventoryFormatter.print(Mockito.any(VariantInventory.class), Mockito.any(Locale.class))).thenReturn("20");