我想在我的应用程序中使用请求范围的bean。我使用JUnit4进行测试。如果我尝试在这样的测试中创建一个:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/TestScopedBeans-context.xml" }) public class TestScopedBeans { protected final static Logger logger = Logger .getLogger(TestScopedBeans.class); @Resource private Object tObj; @Test public void testBean() { logger.debug(tObj); } @Test public void testBean2() { logger.debug(tObj); }
使用以下bean定义:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="java.lang.Object" id="tObj" scope="request" /> </beans>
我得到:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gov.nasa.arc.cx.sor.query.TestScopedBeans': Injection of resource fields failed; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'request' <...SNIP...> Caused by: java.lang.IllegalStateException: No Scope registered for scope 'request'
但是我注意到他使用了AbstractDependencyInjectionSpringContextTests,它似乎在Spring 3.0中已被弃用。我目前使用Spring 2.5,但认为切换此方法以使用Docs建议的使用AbstractJUnit4SpringContextTests应该并不难(确定docs链接到3.8版本,但我使用的是4.4)。所以我更改测试以扩展AbstractJUnit4SpringContextTests …相同的消息。同样的问题。现在,我要覆盖的prepareTestInstance()方法未定义。好吧,也许我将那些registerScope调用放在其他地方…所以我阅读了更多有关TestExecutionListeners的内容,并认为这样做会更好,因为我不想继承spring包结构。因此,我将测试更改为:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/TestScopedBeans-context.xml" }) @TestExecutionListeners({}) public class TestScopedBeans {
期望我必须创建一个自定义侦听器,但是运行它时却是我。有用!很好,但是为什么呢?我看不到任何股票侦听器在哪里注册请求范围或会话范围,为什么呢?现在还没有什么要说的,这可能不是Spring MVC代码的测试…
测试通过,因为它没有执行任何操作:)
省略@TestExecutionListeners注释时,Spring会注册3个默认侦听器,其中一个称为DependencyInjectionTestExecutionListener。该侦听器负责扫描你的测试类以查找要注入的内容,包括@Resource注释。tObj由于未定义范围,此侦听器尝试注入,但失败。
@TestExecutionListeners
DependencyInjectionTestExecutionListener
当你声明时@TestExecutionListeners({}),你将禁止的注册DependencyInjectionTestExecutionListener,因此该测试根本不会被tObj注入,并且由于你的测试未在检查的存在tObj,因此该测试通过了。
@TestExecutionListeners({})
tObj
修改你的测试,使其执行此操作,它将失败:
@Test public void testBean() { assertNotNull("tObj is null", tObj); }
所以空着的话@TestExecutionListeners,测试就通过了,因为什么都没发生。
现在,解决你原来的问题。如果你想尝试在测试上下文中注册请求范围,请查看的源代码WebApplicationContextUtils.registerWebApplicationScopes(),你将找到以下行:
beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
你可以尝试一下,看看你的计划如何,但是可能会有奇怪的副作用,因为你并不是真的打算在测试中这样做。
相反,我建议改写测试,以便你不需要请求作用域的bean。@Test如果你编写自包含的测试,这应该不难,不应该超过请求范围的Bean的生命周期。记住,不需要测试作用域机制,它是Spring的一部分,你可以假设它起作用。
@Test
Spring 3.2或更高版本的解决方案 从3.2版开始的Spring 提供了对会话/请求范围内的bean的支持,以进行集成测试。
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfig.class) @WebAppConfiguration public class SampleTest { @Autowired WebApplicationContext wac; @Autowired MockHttpServletRequest request; @Autowired MockHttpSession session; @Autowired MySessionBean mySessionBean; @Autowired MyRequestBean myRequestBean; @Test public void requestScope() throws Exception { assertThat(myRequestBean) .isSameAs(request.getAttribute("myRequestBean")); assertThat(myRequestBean) .isSameAs(wac.getBean("myRequestBean", MyRequestBean.class)); } @Test public void sessionScope() throws Exception { assertThat(mySessionBean) .isSameAs(session.getAttribute("mySessionBean")); assertThat(mySessionBean) .isSameAs(wac.getBean("mySessionBean", MySessionBean.class)); } }
使用监听器的3.2之前的Spring解决方案
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfig.class) @TestExecutionListeners({WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class}) public class SampleTest { ... }
WebContextTestExecutionListener.java
public class WebContextTestExecutionListener extends AbstractTestExecutionListener { @Override public void prepareTestInstance(TestContext testContext) { if (testContext.getApplicationContext() instanceof GenericApplicationContext) { GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext(); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new SimpleThreadScope()); beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SimpleThreadScope()); } } }
具有自定义范围的3.2之前的Spring解决方案
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfig.class, locations = "test-config.xml") public class SampleTest { ... }
TestConfig.java
@Configuration @ComponentScan(...) public class TestConfig { @Bean public CustomScopeConfigurer customScopeConfigurer(){ CustomScopeConfigurer scopeConfigurer = new CustomScopeConfigurer(); HashMap<String, Object> scopes = new HashMap<String, Object>(); scopes.put(WebApplicationContext.SCOPE_REQUEST, new SimpleThreadScope()); scopes.put(WebApplicationContext.SCOPE_SESSION, new SimpleThreadScope()); scopeConfigurer.setScopes(scopes); return scopeConfigurer }
或使用xml配置
test-config.xml
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> <property name="scopes"> <map> <entry key="request"> <bean class="org.springframework.context.support.SimpleThreadScope"/> </entry> </map> <map> <entry key="session"> <bean class="org.springframework.context.support.SimpleThreadScope"/> </entry> </map> </property> </bean>