在测试方法中重新加载或刷新Spring应用程序上下文? [英] Reload or refresh a Spring application context inside a test method?

查看:773
本文介绍了在测试方法中重新加载或刷新Spring应用程序上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的测试类的单个方法中更改我的applicationContext中活动的Spring配置文件,为此我需要在刷新竞赛之前运行一行代码,因为我使用的是ProfileResolver。我尝试了以下方法:

I need to change the Spring profiles that are active in my applicationContext within a single method of my test class, and to do so I need to run one line of code before refreshing the contest because I am using a ProfileResolver. I have tried the following:

@WebAppConfiguration
@ContextConfiguration(locations = {"/web/WEB-INF/spring.xml"})
@ActiveProfiles(resolver = BaseActiveProfilesResolverTest.class)
public class ControllerTest extends AbstractTestNGSpringContextTests {
    @Test
    public void test() throws Exception {
        codeToSetActiveProfiles(...);
        ((ConfigurableApplicationContext)this.applicationContext).refresh();
        ... tests here ...
        codeToSetActiveProfiles(... back to prior profiles ...);
        ... ideally refresh/reload the context for future tests
    }
}

但是我得到:

java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once

DirtiesContext对我不起作用,因为它是在类/方法执行后运行的,而不是之前,我还需要在运行刷新/重新加载之前执行一行代码。

DirtiesContext does not work for me because it is run AFTER class/method execution, not before, and I need to execute a line of code prior to running the refresh/reload anyway.

有什么建议吗?我试着看一下正在运行的监听器/钩子,但是我没有看到一个明显的位置来插入自己以实现这种行为。

Any suggestions? I tried to have a look through the listeners/hooks that are being run, but I didn't see an obvious location to insert myself to achieve this behavior.

推荐答案

按照设计,Spring TestContext Framework并未明确支持程序化刷新 ApplicationContext 。此外,测试方法并不打算刷新上下文。

By design, programmatic refreshing of an ApplicationContext is not explicitly supported by the Spring TestContext Framework. Furthermore, it is not intended that a test method refresh a context.

因此我建议您重新评估是否需要刷新并考虑替代方法,例如放置需要的测试方法专用测试类中的一组不同的活动配置文件。

Thus I would recommend that you reassess your need for a refresh and consider alternatives like placing test methods that require a different set of active profiles in a dedicated test class.

总之, @ActiveProfiles 支持声明性配置(通过 value profiles attributes)和 programmatic 配置(通过 resolver 属性)测试的活动配置文件,但仅限于测试类级别(不在方法级别)。另一个选择是实现 ApplicationContextInitializer 并通过 @ContextConfiguration(initializers = ...)配置它。

In summary, @ActiveProfiles supports declarative configuration (via value and profiles attributes) and programmatic configuration (via the resolver attribute) of the active profiles for tests, but only at the test class level (not at the method level). Another option is to implement an ApplicationContextInitializer and configure that via @ContextConfiguration(initializers=...).

在刷新之前影响 ApplicationContext 的唯一方法是实现 SmartContextLoader 或扩展其中一个提供的类,并通过 @ContextConfiguration(loader = ...)进行配置。例如, AbstractGenericContextLoader.customizeContext()允许在<之后自定义创建的 GenericApplicationContext / em> bean定义已加载到上下文中,但之前刷新上下文。

The only other way to affect the ApplicationContext before it is refreshed is to implement a SmartContextLoader or extend one of the provided classes and configure it via @ContextConfiguration(loader=...). For example, AbstractGenericContextLoader.customizeContext() allows one to "customize the GenericApplicationContext created by the loader after bean definitions have been loaded into the context but before the context is refreshed."

祝你好运,

Sam(Spring TestContext框架的作者)

Sam (author of the Spring TestContext Framework)

这篇关于在测试方法中重新加载或刷新Spring应用程序上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆