【问题标题】:Issue with maven + surefire-plugin and Spring @Autowiredmaven + surefire-plugin 和 Spring @Autowired 的问题
【发布时间】:2012-04-27 19:34:44
【问题描述】:

我一直在努力让 Spring @Autowired 在 maven 测试中工作。当我在 IntellJ 中运行 JUnit 测试(没有尝试 Eclipse)时,它可以工作。但是当我运行 mvn clean install 时,JUnit 测试失败并出现以下错误

testApp(com.sample.spring.AppTest): 创建名为 'com.sample.spring.AppTest' 的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.sample.spring.AppB com.sample.spring.AppTest.appB;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为 [com.sample.spring.AppB] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

我创建了一个自包含的示例项目,该项目始终表现出这种行为。我正在使用 Spring 3.1.1。我确信有人遇到了同样的问题并破解了它。寻找有关此问题的一些指示。

【问题讨论】:

标签: spring maven-3 junit4 maven-surefire-plugin


【解决方案1】:

这是一个构建路径问题:

在 Eclipse 中工作,我将您的 ContextConfiguration 更改为:

@ContextConfiguration(locations = "classpath:applicationContext.xml")

它同时使用标准的 eclipse runner 和 maven(maven 测试或 maven 安装)运行。

确保你有:

src/main/java

src/test/java 和

src/test/resources

在构建路径中声明为源文件夹

【讨论】:

  • 感谢您的帮助。我也很头疼,意识到我的 src/test/resources 是问题所在。我在 src/test/java/resources 有它,你也指出了这一点。
猜你喜欢
  • 1970-01-01
  • 2016-03-05
  • 2011-03-31
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2016-10-02
  • 2014-06-28
  • 2020-11-09
相关资源
最近更新 更多