首页 > 解决方案 > 如何在没有 WebAppConfiguration 的集成测试中模拟 Spring 会话

问题描述

我需要执行一个依赖会话 bean 的集成测试。但是,我不想使用 @WepAppConfiguration 因为它非常耗时。

目前,我使用 SpringRunner 和 ContextConfiguration 来解决 bean 依赖关系,测试执行得非常快。但是,我需要一些代码来模拟会话 bean,我想在不使用 @WebAppConfiguration 的情况下这样做,因为它非常耗时。

测试类:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestContextSpringConfig.class)
public class MethodTest {

    @Autowired
    private BeanSession beanSession;
}

弹簧配置类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("net.foo")
public class TestContextSpringConfig {

    @Bean // Fancy bean
    public ConfigurationPackages configurationPackages() {
        return new ConfigurationPackages();
    }

    // How to mock the session bean?

}

我希望测试能够执行而不会引发以下错误:

java.lang.IllegalStateException: No Scope registered for scope name 'session'

标签: springjunitspring-test

解决方案


我找到了答案。响应是这个 POST stackoverflow和这个memorynotfound的混合。

在此设置中,我为模拟“会话”范围的每个 Junit 测试方法启动一个线程范围。我所做的是创建以下配置:

@Configuration
@ComponentScan("net.foo")
public class TestContextSpringConfig {

    @Bean
    public ConfigurationPackages configurationPackages() {
        return new ConfigurationPackages();
    }

    @Bean
    public CustomScopeConfigurer customScope() {
        CustomScopeConfigurer configurer = new CustomScopeConfigurer();
        Map<String, Object> sessionScope = new HashMap<>();
        sessionScope.put("session", new ThreadScope());
        configurer.setScopes(sessionScope);
        return configurer;
    }
}

以下线程范围:

package net.weg.maestro;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
import org.springframework.core.NamedThreadLocal;
import java.util.HashMap;
import java.util.Map;

public class ThreadScope implements Scope {

    private final ThreadLocal<Map<String, Object>> threadScope =
            new NamedThreadLocal<Map<String, Object>>(ThreadScope.class.getName()) {
                @Override
                protected Map<String, Object> initialValue() {
                    return new HashMap<String, Object>();
                }
            };

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        Map<String, Object> scope = this.threadScope.get();
        Object object = scope.get(name);
        if (object == null) {
            object = objectFactory.getObject();
            scope.put(name, object);
        }
        return object;
    }

    @Override
    public Object remove(String name) {
        Map<String, Object> scope = this.threadScope.get();
        return scope.remove(name);
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {
    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }

    @Override
    public String getConversationId() {
        return Thread.currentThread().getName();
    }

    public void clear(){
        Map<String, Object> scope = this.threadScope.get();
        scope.clear();
    }
}

并且单元测试现在可以访问沙盒环境 (ThreadLocal) 中的会话 bean。

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestContextSpringConfig.class)
public class MethodTest {

    @Autowired
    private BeanSession beanSession; // Session bean

@Test
    public void parameterReachabilityTest() {
        ObjectA objectA = new ObjectA();
        ObjectB objectB = new ObjectB();
        objectA.getObjectBList().add(objectB);
        objectB.setObjectA(objectA);
        beanSession.setRootState(objectA); // Using session bean
        ObjectBComponent objectBComponent = maestro.getComp(objectB, ObjectBComponent.class);
        objectBComponent.parameterReachableTest();
        assertThat((Object) objectBComponent.getThreadValue("objectBComponent")).isNotNull();
        assertThat((Object) objectBComponent.getThreadValue("objectAComponent")).isNotNull();
    }
}

推荐阅读