spring - 如何在没有 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'
解决方案
我找到了答案。响应是这个 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();
}
}
推荐阅读
- python - Sklearn LassoCV回归模型预测的置信区间
- javascript - 什么是“函数 f(a = {} = {} = {}) {}”
- php - laravel 中访问会话数据时读取的是哪个会话文件?
- regex - 如何使用ansible复制匹配正则表达式的文件
- sql - 动态日期查找
- php - 检测何时仅剩一行空间以及何时使用 FPDF 添加新页面
- javascript - 函数如何使用 [[FunctionLocation]] 访问外部脚本?
- html - 如何在不使用 gsub 的情况下转义“
- machine-learning - 为什么这个简单的 Keras 模型有两个参数?
- azure - Azure PostgreSQL 服务器没有 VNET 选项