【问题标题】:Spring unit test - using @ContextConfiguration when extending class in other projectSpring单元测试-在其他项目中扩展类时使用@ContextConfiguration
【发布时间】:2012-12-03 16:21:44
【问题描述】:

我有一个弹簧单元测试,它扩展了位于其他项目中的其他测试(tests-common)。 我的上下文文件 (my-context.xml) 与 MyTest 位于同一个包中。 基本上下文文件(“base-context.xml”)与 BaseTest 位于同一包中。 文件看起来像:

@ContextConfiguration(locations = { "my-context.xml"}) 
public class MyTest extends BaseTest {

@ContextConfiguration(locations = { "base-context.xml" })
public abstract class BaseTest extends AbstractJUnit4SpringContextTests{

问题是:BaseTest 找到“base-context.xml”,但 MyTest 找到“my-context.xml”的 FileNotFoundException。 如果我将 BaseTest 和 my-context.xml 文件移动到与 BaseTest 相同的包中 - 一切正常。 可能是什么问题?

【问题讨论】:

  • 您可以尝试在文件名前加上classpath: 前缀吗?

标签: spring unit-testing junit


【解决方案1】:

在 Spring 文档中查看此内容:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-app-ctx

我会在 base-context.xml 前面添加“classpath:”关键字并定义相对于类路径的路径。 您还应该将您的 xml 文件放在子目录 src/main/resources 中,而不是放在 java 文件旁边。

【讨论】:

    【解决方案2】:

    如下所述,您可以像这样使用 classpath 关键字:

    @ContextConfiguration(locations = { "classpath:**/base-context.xml" })
    

    【讨论】:

      猜你喜欢
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多