使用@ContextConfiguration(loader=MyLoader.class)时如何只使用一个spring测试上下文

How to use only one spring test context when @ContextConfiguration(loader=MyLoader.class) is used

我需要控制测试上下文的创建。因此,对于我的测试 classes,我将 @ContextConfiguration(loader=MyLoader.class) 设置为 MyLoader extends AnnotationConfigContextLoader。问题在于,对于每个测试,都会创建 class MyLoader 实例,并且该加载器实例会创建新的上下文。所以,我对每个测试都有一个新的上下文 class。但是,这不是我需要的。是否可以让 spring-test 为所有测试 classes 同时控制加载过程只创建一个上下文?

为了清楚起见,我使用加载程序,因为我需要动态设置上下文配置 class。我没有找到办法:

@ContextConfiguration(classProvider=ContextConfigClassProvider.class)//this is what I need
public class FooServiceIT {

    @Test
    public void t() {
        System.out.println("TEST");
    }
}

所以,我使用加载程序。

Spring 测试框架已经缓存了加载上下文的结果。重要的是缓存键由几个部分构成(参见 the reference guide)。只要这些部分相同,它就会使用已经加载的缓存,而不会重新加载。