org.springframework.mock.web.test.MockRequestDispatcher Java Examples

The following examples show how to use org.springframework.mock.web.test.MockRequestDispatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposedContextBeanNames(new String[] {"myBean2"});
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertNull(forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example #2
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public RequestDispatcher getNamedDispatcher(String path) {
	if (path.equals("default") || path.equals("custom")) {
		return new MockRequestDispatcher("/");
	}
	else {
		return null;
	}
}
 
Example #3
Source File: InternalResourceViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void includeOnCommitted() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	response.setCommitted(true);
	view.setUrl(url);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
}
 
Example #4
Source File: InternalResourceViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void includeOnAttribute() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somepath");
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	view.setUrl(url);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
}
 
Example #5
Source File: InternalResourceViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void alwaysInclude() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	view.setUrl(url);
	view.setAlwaysInclude(true);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
}
 
Example #6
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map map = new HashMap();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposedContextBeanNames(new String[] {"myBean2"});
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertNull(forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap(), request, response);
}
 
Example #7
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map map = new HashMap();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposeContextBeansAsAttributes(true);
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertSame(wac.getBean("myBean"), forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap(), request, response);
}
 
Example #8
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public RequestDispatcher getNamedDispatcher(String path) {
	if (path.equals("default") || path.equals("custom")) {
		return new MockRequestDispatcher("/");
	}
	else {
		return null;
	}
}
 
Example #9
Source File: InternalResourceViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void includeOnCommitted() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	response.setCommitted(true);
	view.setUrl(url);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.forEach((k, v) -> verify(request).setAttribute(k, v));
}
 
Example #10
Source File: InternalResourceViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void includeOnAttribute() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somepath");
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	view.setUrl(url);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.forEach((key, value) -> verify(request).setAttribute(key, value));
}
 
Example #11
Source File: InternalResourceViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void alwaysInclude() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	view.setUrl(url);
	view.setAlwaysInclude(true);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.forEach((key, value) -> verify(request).setAttribute(key, value));
}
 
Example #12
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposedContextBeanNames(new String[] {"myBean2"});
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertNull(forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example #13
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposeContextBeansAsAttributes(true);
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertSame(wac.getBean("myBean"), forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example #14
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public RequestDispatcher getNamedDispatcher(String path) {
	if (path.equals("default") || path.equals("custom")) {
		return new MockRequestDispatcher("/");
	}
	else {
		return null;
	}
}
 
Example #15
Source File: InternalResourceViewTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void includeOnCommitted() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	response.setCommitted(true);
	view.setUrl(url);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.forEach((k, v) -> verify(request).setAttribute(k, v));
}
 
Example #16
Source File: InternalResourceViewTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void includeOnAttribute() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somepath");
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	view.setUrl(url);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.forEach((key, value) -> verify(request).setAttribute(key, value));
}
 
Example #17
Source File: InternalResourceViewTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void alwaysInclude() throws Exception {
	given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));

	view.setUrl(url);
	view.setAlwaysInclude(true);

	// Can now try multiple tests
	view.render(model, request, response);
	assertEquals(url, response.getIncludedUrl());

	model.forEach((key, value) -> verify(request).setAttribute(key, value));
}
 
Example #18
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposeContextBeansAsAttributes(true);
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertSame(wac.getBean("myBean"), forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example #19
Source File: DefaultServletHandlerConfigurerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public RequestDispatcher getNamedDispatcher(String url) {
	this.url = url;
	return new MockRequestDispatcher(url);
}
 
Example #20
Source File: DefaultServletHandlerConfigurerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public RequestDispatcher getNamedDispatcher(String url) {
	this.url = url;
	return new MockRequestDispatcher(url);
}
 
Example #21
Source File: DefaultServletHandlerConfigurerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public RequestDispatcher getNamedDispatcher(String url) {
	this.url = url;
	return new MockRequestDispatcher(url);
}