如何在没有 WebAppConfiguration 的情况下在集成测试中模拟 Spring 会话
How to mock Spring session in integration test without WebAppConfiguration
我需要执行依赖于会话 bean 的集成测试。但是,我不想使用 @WepAppConfiguration 因为它太耗时了。
目前,我正在使用 SpringRunner 和 ContextConfiguration 来解决 beans 依赖关系,测试执行得非常快。但是,我需要一些代码来模拟会话 bean,我想在不使用 @WebAppConfiguration 的情况下做到这一点,因为它非常耗时。
测试class:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestContextSpringConfig.class)
public class MethodTest {
@Autowired
private BeanSession beanSession;
}
Spring配置class:
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 Whosebug with this one memorynotfound.
的混合
在此设置中,我为模拟 "session" 范围的 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();
}
}
我需要执行依赖于会话 bean 的集成测试。但是,我不想使用 @WepAppConfiguration 因为它太耗时了。
目前,我正在使用 SpringRunner 和 ContextConfiguration 来解决 beans 依赖关系,测试执行得非常快。但是,我需要一些代码来模拟会话 bean,我想在不使用 @WebAppConfiguration 的情况下做到这一点,因为它非常耗时。
测试class:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestContextSpringConfig.class)
public class MethodTest {
@Autowired
private BeanSession beanSession;
}
Spring配置class:
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 Whosebug with this one memorynotfound.
的混合在此设置中,我为模拟 "session" 范围的 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();
}
}