【问题标题】:How to exclude a spring context of a project which depends on another project while testing如何在测试时排除依赖于另一个项目的项目的弹簧上下文
【发布时间】:2014-05-14 13:27:54
【问题描述】:

我有两个 Java 项目,“A”和“B”,B 对 A 有 Maven 依赖项。我开始为 B 创建测试,但是当我尝试运行测试时,它不起作用。 错误是在测试 B 时,我的测试使用了 A 的 spring 上下文。

知道如何从 B 的测试中排除 A 上下文吗? 我正在使用它来设置 B 的测试上下文

@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {BTestContext.class})

我尝试在 BTestContext 中使用排除过滤器,但没有奏效。

@ComponentScan(basePackages = {"com.foo.b"}, excludeFilters = @ComponentScan.Filter(value = ABeanConfig.class))

【问题讨论】:

    标签: java spring unit-testing


    【解决方案1】:

    以下文档指出,您可以为 @ContextConfiguration 注解指定 customerContextLoader 或 customContextInitializer。

    http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html#integration-testing-annotations

    一个建议是创建一个自定义的 ContextLoader。

    对于基于注释的配置,您可能希望覆盖以下 1 个或多个方法:

    http://docs.spring.io/spring/docs/3.2.8.RELEASE/javadoc-api/org/springframework/test/context/support/AnnotationConfigContextLoader.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 2022-10-20
      • 1970-01-01
      • 2017-07-05
      相关资源
      最近更新 更多