Search in sources :

Example 6 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class ModelAndViewResolverMethodReturnValueHandlerTests method modelAndViewResolver.

@Test
public void modelAndViewResolver() throws Exception {
    MethodParameter returnType = new MethodParameter(getClass().getDeclaredMethod("testBeanReturnValue"), -1);
    mavResolvers.add(new TestModelAndViewResolver(TestBean.class));
    TestBean testBean = new TestBean("name");
    handler.handleReturnValue(testBean, returnType, mavContainer, request);
    assertThat(mavContainer.getViewName()).isEqualTo("viewName");
    assertThat(mavContainer.getModel().get("modelAttrName")).isSameAs(testBean);
    assertThat(mavContainer.isRequestHandled()).isFalse();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 7 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class DispatcherServletTests method noCleanupAfterInclude.

@Test
public void noCleanupAfterInclude() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute("test1", "value1");
    request.setAttribute("test2", "value2");
    WebApplicationContext wac = new StaticWebApplicationContext();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    TestBean command = new TestBean();
    request.setAttribute("command", command);
    request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
    simpleDispatcherServlet.setCleanupAfterInclude(false);
    simpleDispatcherServlet.service(request, response);
    assertThat(request.getAttribute("test1")).isEqualTo("value1");
    assertThat(request.getAttribute("test2")).isEqualTo("value2");
    assertThat(request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE)).isSameAs(wac);
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 8 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleMethodArgumentNotValid.

@Test
public void handleMethodArgumentNotValid() throws Exception {
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean");
    errors.rejectValue("name", "invalid");
    MethodParameter parameter = new MethodParameter(this.getClass().getMethod("handle", String.class), 0);
    MethodArgumentNotValidException ex = new MethodArgumentNotValidException(parameter, errors);
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertThat(mav).as("No ModelAndView returned").isNotNull();
    assertThat(mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
    assertThat(response.getStatus()).as("Invalid status code").isEqualTo(400);
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.beans.testfixture.beans.TestBean) ModelAndView(org.springframework.web.servlet.ModelAndView) MethodParameter(org.springframework.core.MethodParameter) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 9 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class RedirectAttributesModelMapTests method mergeAttributes.

@Test
void mergeAttributes() {
    Map<String, Object> map = new HashMap<>();
    map.put("person", new TestBean("Fred"));
    map.put("age", 33);
    this.redirectAttributes.addAttribute("person", new TestBean("Ralph"));
    this.redirectAttributes.mergeAttributes(map);
    assertThat(this.redirectAttributes.get("person")).isEqualTo("Ralph");
    assertThat(this.redirectAttributes.get("age")).isEqualTo("33");
}
Also used : HashMap(java.util.HashMap) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 10 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class RedirectAttributesModelMapTests method putAll.

@Test
void putAll() {
    Map<String, Object> map = new HashMap<>();
    map.put("person", new TestBean("Fred"));
    map.put("age", 33);
    this.redirectAttributes.putAll(map);
    assertThat(this.redirectAttributes.get("person")).isEqualTo("Fred");
    assertThat(this.redirectAttributes.get("age")).isEqualTo("33");
}
Also used : HashMap(java.util.HashMap) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Aggregations

TestBean (org.springframework.beans.testfixture.beans.TestBean)808 Test (org.junit.jupiter.api.Test)765 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)472 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)268 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)183 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)167 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)162 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)118 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)96 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)40 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)40 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)39 Properties (java.util.Properties)38 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)37 HashMap (java.util.HashMap)36 Errors (org.springframework.validation.Errors)35 PropertyEditorSupport (java.beans.PropertyEditorSupport)34 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 List (java.util.List)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28