31

我想在我的应用程序中使用请求范围的 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'

所以我发现这个博客似乎很有帮助: http ://www.javathinking.com/2009/06/no-scope-registered-for-scope-request_5.html

但我注意到他使用了AbstractDependencyInjectionSpringContextTests,这在 Spring 3.0 中似乎已被弃用。我此时使用 Spring 2.5,但认为按照文档建议切换此方法以使用 AbstractJUnit4SpringContextTests 应该不会太难(好的文档链接到 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 代码的测试......

4

8 回答 8

56

Spring 3.2 或更高版本的解决方案

Spring 从 3.2 版开始为集成测试提供会话/请求范围 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));
    }
}

阅读更多:请求和会话范围的 Bean


Spring 3.2 之前的解决方案,带监听器

@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>

源代码

所有提供的解决方案的源代码:

于 2011-01-11T19:59:47.450 回答
9

我尝试了几种解决方案,包括@Marius 使用“WebContextTestExecutionListener”的解决方案,但它对我不起作用,因为此代码在创建请求范围之前加载了应用程序上下文。

最后帮助我的答案不是新的,但很好: http ://tarunsapra.wordpress.com/2011/06/28/junit-spring-session-and-request-scope-beans/

我只是将以下代码段添加到我的(测试)应用程序上下文中:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="request">
                <bean class="org.springframework.context.support.SimpleThreadScope"/>
            </entry>
        </map>
    </property>
</bean>

祝你好运!

于 2013-03-19T15:42:48.133 回答
8

测试通过了,因为它什么也没做 :)

当您省略@TestExecutionListeners注解时,Spring 会注册 3 个默认侦听器,包括一个名为DependencyInjectionTestExecutionListener. 这是负责扫描您的测试类以查找要注入的内容(包括@Resource注释)的侦听器。由于未定义的范围,此侦听器尝试注入tObj并失败。

当您声明 时@TestExecutionListeners({}),您会抑制 的注册DependencyInjectionTestExecutionListener,因此测试根本不会被tObj注入,并且因为您的测试没有检查 的存在tObj,所以它通过了。

修改你的测试,让它这样做,它会失败:

@Test
public void testBean() {
    assertNotNull("tObj is null", tObj);
}

所以你的空@TestExecutionListeners,测试通过,因为没有任何反应

现在,回到你原来的问题。如果您想尝试在测试上下文中注册请求范围,请查看源代码WebApplicationContextUtils.registerWebApplicationScopes(),您会发现以下行:

beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());

你可以尝试一下,看看你会怎么做,但可能会有奇怪的副作用,因为你并不是真的打算在测试中这样做。

相反,我建议改写你的测试,这样你就不需要请求范围的 bean。这应该不难,@Test如果您编写自包含测试,则生命周期不应长于请求范围 bean 的生命周期。请记住,无需测试作用域机制,它是 Spring 的一部分,您可以假设它有效。

于 2010-03-09T21:59:23.030 回答
8

一个使用 Spring 4 测试的解决方案,用于当您需要请求范围的 bean 但不通过MockMVC等发出任何请求时。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(/* ... */)
public class Tests {

    @Autowired
    private GenericApplicationContext context;

    @Before
    public void defineRequestScope() {
        context.getBeanFactory().registerScope(
            WebApplicationContext.SCOPE_REQUEST, new RequestScope());
        RequestContextHolder.setRequestAttributes(
            new ServletRequestAttributes(new MockHttpServletRequest()));
    }

    // ...
于 2016-06-08T10:05:49.123 回答
2

这仍然是一个悬而未决的问题:

https://jira.springsource.org/browse/SPR-4588

通过定义一个自定义上下文加载器,我能够(主要)让它工作,如

http://forum.springsource.org/showthread.php?p=286280

于 2011-02-10T19:06:28.427 回答
2

Test Request-Scoped Beans with Spring很好地解释了如何使用 Spring 注册和创建自定义范围。

简而言之,正如 Ido Cohn 所解释的,将以下内容添加到文本上下文配置中就足够了:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="request">
                <bean class="org.springframework.context.support.SimpleThreadScope"/>
            </entry>
        </map>
    </property>
</bean>

无需使用基于 ThreadLocal 的预定义 SimpleThreadScope,还可以轻松实现自定义的,如文章中所述。

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

public class CustomScope implements Scope {

    private final Map<String , Object> beanMap = new HashMap<String , Object>();

    public Object get(String name, ObjectFactory<?> factory) {
        Object bean = beanMap.get(name);
        if (null == bean) {
            bean = factory.getObject();
            beanMap.put(name, bean);
        }
        return bean;
    }

    public String getConversationId() {
        // not needed
        return null;
    }

    public void registerDestructionCallback(String arg0, Runnable arg1) {
        // not needed
    }

    public Object remove(String obj) {
        return beanMap.remove(obj);
    }

    public Object resolveContextualObject(String arg0) {
        // not needed
        return null;
    }
}
于 2013-11-25T11:00:05.590 回答
1

MariuszS 的解决方案有效,但我无法正确提交事务。

似乎新发布的 3.2 终于使测试请求/会话范围的 bean 成为一等公民。这里有几个博客了解更多详细信息。

Rossen Stoyanchev 的Spring Framework 3.2 RC1:Spring MVC 测试框架

Sam Brannen 的Spring Framework 3.2 RC1:新的测试功能

于 2012-12-20T03:50:33.370 回答
0

不阅读文档有时会让人发疯。几乎。

如果您使用的是寿命较短的 bean(例如请求范围),您很可能还需要更改惰性初始化默认值!否则 WebAppContext 将无法加载并告诉您有关缺少请求范围的信息,这当然是缺少的,因为上下文仍在加载!

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-lazy-init

Spring 家伙绝对应该将这个提示放入他们的异常消息中......

如果不想更改默认值,还有注解方式:在@Component 等之后加上“@Lazy(true)”,使单例初始化惰性,避免过早实例化请求范围的bean。

于 2012-08-08T10:04:05.823 回答