【问题标题】:Make ApplicationContext dirty before and after test class在测试类之前和之后使 ApplicationContext 变脏
【发布时间】:2016-09-01 16:55:32
【问题描述】:

我的 Spring 集成测试中有一个特定的类(比如说MyTest),它在 Spring 组件上使用 PowerMock @PrepareForTest 注释:@PrepareForTest(MyComponent.class)。这意味着 PowerMock 将加载这个类并进行一些修改。问题是,我的@ContextConfiguration 定义在由MyTest 扩展的超类上,而ApplicationContext 缓存在不同的测试类之间。现在,如果首先运行MyTest,它将具有MyComponent 的正确PowerMock 版本,但如果不是,则测试将失败,因为将为另一个测试加载上下文(没有@PrepareForTest)。

所以我想做的是在MyTest 之前重新加载我的上下文。我可以通过

@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)

但是如果我还想在测试完成后重新加载上下文怎么办?所以我将再次拥有干净的MyComponent,而无需修改 PowerMock。有没有办法同时做到BEFORE_CLASSAFTER_CLASS

现在我用以下 hack 做到了:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

在 MyTest 上,然后

/**
 * Stub test to reload ApplicationContext before execution of real test methods of this class.
 */
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
@Test
public void aa() {
}

/**
 * Stub test to reload ApplicationContext after execution of real test methods of this class.
 */
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
@Test
public void zz() {
}

我想知道是否有更漂亮的方法可以做到这一点?

作为一个附带问题,是否可以只重新加载某些 bean 而不是完整的上下文?

【问题讨论】:

  • 为什么要在测试之前将上下文标记为脏?
  • 如前所述,我想为 MyTest 类重新加载上下文,所以我的 @PrepareForTest(MyComponent.class) 将起作用。

标签: java spring spring-test


【解决方案1】:

有没有办法同时做 BEFORE_CLASS 和 AFTER_CLASS?

不,很遗憾,@DirtiesContext 不支持。

然而,你真正想说的是你想要一个新的ApplicationContext 用于MyTest,它与父测试类的上下文相同,但只与MyTest 一样长。而且...您不想影响为父测试类缓存的上下文。

因此,考虑到这一点,以下技巧应该可以完成这项工作。

@RunWith(SpringJUnit4ClassRunner.class)
// Inherit config from parent and combine with local 
// static Config class to create a new context
@ContextConfiguration
@DirtiesContext
public class MyTest extends BaseTests {

    @Configuration
    static class Config {
        // No need to define any actual @Bean methods.
        // We only need to add an additional @Configuration
        // class so that we get a new ApplicationContext.
    }
}

@DirtiesContext 的替代方案

如果您希望在测试类之前之后 都弄脏上下文,您可以实现一个自定义的TestExecutionListener 来做到这一点。例如,以下内容就可以解决问题。

import org.springframework.core.Ordered;
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;

public class DirtyContextBeforeAndAfterClassTestExecutionListener
        extends AbstractTestExecutionListener {

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

    @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
        testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE);
    }

    @Override
    public void afterTestClass(TestContext testContext) throws Exception {
        testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE);
    }

}

然后您可以使用MyTest 中的自定义侦听器,如下所示。

import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestExecutionListeners.MergeMode;

@TestExecutionListeners(
    listeners = DirtyContextBeforeAndAfterClassTestExecutionListener.class, 
    mergeMode = MergeMode.MERGE_WITH_DEFAULTS
)
public class MyTest extends BaseTest { /* ... */ }

作为一个附带问题,是否可以仅重新加载某些 bean 而不是完整的上下文?

不,那也不可能。

问候,

Sam(Spring TestContext 框架的作者

【讨论】:

  • 感谢您的回复。 you want a new ApplicationContext for MyTest that is identical to the context for the parent test class 这不是真的,因为 MyTest 的上下文不会完全相同,因为它应该使用 @PrepareForTest(MyComponent.class)。我尝试了您的建议,但遇到了另一个问题。据我了解,将有两个并行的上下文,这是目前不可能的(配置了一个 Hazelcast 实例,一个端口上只能有一个)。我可以处理这个问题,所以我很好奇如何让我的 Powemock 注释在这个嵌套上下文中工作?
  • 我通过添加自定义 TestExecutionListener 更新了我的答案,该自定义 TestExecutionListener 在测试类之前和之后弄脏了上下文。让我知道这是否能解决您的问题。
  • 是的,确实如此,而且看起来比 hack 好多了 :) 非常感谢!
  • @SamBrannen 我已经以这种方式实现,并且在这样做之后开始使用循环依赖语句获取 beanInitializationException,如果我不在我的子类中使用侦听器,情况并非如此。在 spring 测试文档页面docs.spring.io/spring/docs/current/spring-framework-reference/… 上,我看到很少的配置建议,如果我正在为一个测试套件做,我也在我的一半测试套件中使用这个监听器,o 我需要在我的 jar 中放置一个文件 spring.factory自定义侦听器条目。
  • @SamBrannen 我已经以这种方式实现了,在做错误之后是dzone.com/articles/resolve-circular-dependency,如果我不在我的子类中使用监听器,情况就不是这样了。在 spring 测试文档页面docs.spring.io/spring/docs/current/spring-framework-reference/… 上,我看到很少的配置建议,如果我正在为一个测试套件做,我也在我的一半 testSuite 中使用这个监听器,我需要在我的 jar 中放入一个文件 spring.factory自定义侦听器条目。
猜你喜欢
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2016-03-26
  • 2017-02-11
  • 2017-02-14
  • 2015-05-30
相关资源
最近更新 更多