@Test public void resolveArgumentOrdering() throws Exception { String name = "testBean"; Object testBean = new TestBean(name); mavContainer.addAttribute(name, testBean); mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, testBean); Object anotherTestBean = new TestBean(); mavContainer.addAttribute("anotherTestBean", anotherTestBean); StubRequestDataBinder dataBinder = new StubRequestDataBinder(testBean, name); WebDataBinderFactory binderFactory = createMock(WebDataBinderFactory.class); expect(binderFactory.createBinder(webRequest, testBean, name)).andReturn(dataBinder); replay(binderFactory); processor.resolveArgument(paramModelAttr, mavContainer, webRequest, binderFactory); assertSame( "Resolved attribute should be updated to be last in the order", testBean, mavContainer.getModel().values().toArray()[1]); assertSame( "BindingResult of resolved attribute should be last in the order", dataBinder.getBindingResult(), mavContainer.getModel().values().toArray()[2]); }
@Test public void resovleArgumentViaDefaultConstructor() throws Exception { WebDataBinder dataBinder = new WebRequestDataBinder(null); WebDataBinderFactory factory = createMock(WebDataBinderFactory.class); expect(factory.createBinder((NativeWebRequest) anyObject(), notNull(), eq("attrName"))) .andReturn(dataBinder); replay(factory); processor.resolveArgument(paramNamedValidModelAttr, mavContainer, webRequest, factory); verify(factory); }
private void getAttributeFromModel(String expectedAttributeName, MethodParameter param) throws Exception { Object target = new TestBean(); mavContainer.addAttribute(expectedAttributeName, target); WebDataBinder dataBinder = new WebRequestDataBinder(target); WebDataBinderFactory factory = createMock(WebDataBinderFactory.class); expect(factory.createBinder(webRequest, target, expectedAttributeName)).andReturn(dataBinder); replay(factory); processor.resolveArgument(param, mavContainer, webRequest, factory); verify(factory); }
@Test(expected = BindException.class) public void resovleArgumentBindException() throws Exception { String name = "testBean"; Object target = new TestBean(); mavContainer.getModel().addAttribute(target); StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name); dataBinder.getBindingResult().reject("error"); WebDataBinderFactory binderFactory = createMock(WebDataBinderFactory.class); expect(binderFactory.createBinder(webRequest, target, name)).andReturn(dataBinder); replay(binderFactory); processor.resolveArgument(paramNonSimpleType, mavContainer, webRequest, binderFactory); }
@Test public void resovleArgumentValidation() throws Exception { String name = "attrName"; Object target = new TestBean(); mavContainer.addAttribute(name, target); StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name); WebDataBinderFactory binderFactory = createMock(WebDataBinderFactory.class); expect(binderFactory.createBinder(webRequest, target, name)).andReturn(dataBinder); replay(binderFactory); processor.resolveArgument(paramNamedValidModelAttr, mavContainer, webRequest, binderFactory); assertTrue(dataBinder.isBindInvoked()); assertTrue(dataBinder.isValidateInvoked()); }