use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.
the class ServletContextSupportTests method testServletContextAttributeExporter.
@Test
public void testServletContextAttributeExporter() {
TestBean tb = new TestBean();
Map<String, Object> attributes = new HashMap<>();
attributes.put("attr1", "value1");
attributes.put("attr2", tb);
MockServletContext sc = new MockServletContext();
ServletContextAttributeExporter exporter = new ServletContextAttributeExporter();
exporter.setAttributes(attributes);
exporter.setServletContext(sc);
assertThat(sc.getAttribute("attr1")).isEqualTo("value1");
assertThat(sc.getAttribute("attr2")).isSameAs(tb);
}
use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.
the class ServletContextSupportTests method testServletContextAttributeFactoryBean.
@Test
@SuppressWarnings("resource")
public void testServletContextAttributeFactoryBean() {
MockServletContext sc = new MockServletContext();
sc.setAttribute("myAttr", "myValue");
StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
wac.setServletContext(sc);
PropertyValues pvs = new PropertyValues();
pvs.add("attributeName", "myAttr");
wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
wac.refresh();
Object value = wac.getBean("importedAttr");
assertThat(value).isEqualTo("myValue");
}
use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.
the class ServletContextSupportTests method testServletContextAttributeFactoryBeanWithAttributeNotFound.
@Test
@SuppressWarnings("resource")
public void testServletContextAttributeFactoryBeanWithAttributeNotFound() {
MockServletContext sc = new MockServletContext();
StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
wac.setServletContext(sc);
PropertyValues pvs = new PropertyValues();
pvs.add("attributeName", "myAttr");
wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(wac::refresh).havingCause().isInstanceOf(IllegalStateException.class).withMessageContaining("myAttr");
}
use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.
the class ServletContextSupportTests method testServletContextParameterFactoryBean.
@Test
@SuppressWarnings("resource")
public void testServletContextParameterFactoryBean() {
MockServletContext sc = new MockServletContext();
sc.addInitParameter("myParam", "myValue");
StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
wac.setServletContext(sc);
PropertyValues pvs = new PropertyValues();
pvs.add("initParamName", "myParam");
wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs);
wac.refresh();
Object value = wac.getBean("importedParam");
assertThat(value).isEqualTo("myValue");
}
use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.
the class ServletContextSupportTests method testServletContextParameterFactoryBeanWithAttributeNotFound.
@Test
@SuppressWarnings("resource")
public void testServletContextParameterFactoryBeanWithAttributeNotFound() {
MockServletContext sc = new MockServletContext();
StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
wac.setServletContext(sc);
PropertyValues pvs = new PropertyValues();
pvs.add("initParamName", "myParam");
wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(wac::refresh).havingCause().isInstanceOf(IllegalStateException.class).withMessageContaining("myParam");
}
Aggregations