您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在spring测试中要求范围内的bean

在spring测试中要求范围内的bean

测试通过,因为它没有执行任何操作:)

省略@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的一部分,你可以假设它起作用。

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>
Java 2022/1/1 18:16:06 有344人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶