I found the answer. The response is a mix of this POST stackoverflow with this one memorynotfound.
In this setting, I starts a thread scope for each method of Junit test simulating a "session" scope. What I've done was to create the following configuration:
@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;
}
}
The following ThreadScope:
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();
}
}
And the unit tests now have access to session beans in a sandbox environment (ThreadLocal).
@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();
}
}