【问题标题】:Spring jUnit Testing - either can not autowire or can not find appContext.xmlSpring jUnit 测试 - 无法自动装配或找不到 appContext.xml
【发布时间】:2011-10-27 09:55:41
【问题描述】:

我正在对基于 spring 的应用程序 atm 进行单元测试。 首先问题是,如果我没有在服务器上启动应用程序一次,单元测试都会失败。 如果我确实首先在服务器上启动应用程序(然后停止它),那么我的单元测试正在运行。

没有启动服务器我得到以下错误:

... java.io.FileNotFoundException: class path resource [META-INF/spring/applicationContext-test.xml] cannot be opened because it does not exist

我的单元测试定义如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext-test.xml" })
@TransactionConfiguration
@Transactional
public class InventoryControllerTest extends AbstractTransactionalJUnit4SpringContextTests {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    private AnnotationMethodHandlerAdapter handlerAdapter;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        handlerAdapter = applicationContext
            .getBean(AnnotationMethodHandlerAdapter.class);
}
    //... tests
}

就像我说的,如果我之前启动过该应用程序,一切正常。

所以我将配置位置更改为 位置= {“类路径/META-INF/spring/applicationContext-test.xml”}) 但不费吹灰之力,与上述相同的异常。

要想走得更远,唯一的方法就是这个位置: 位置 = { "classpath*:applicationContext-test.xml" }) 然后我得到这个异常: 没有为依赖项找到类型为 [javax.sql.DataSource] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注释:{}

但这很令人困惑,因为我的测试上下文文件中肯定有一个数据源:

<bean class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:testdb;sql.syntax_ora=true" />
    <property name="username" value="some" />
    <property name="password" value="some" />
</bean>

EIDT 2

认识到问题在于 RunWith(...) 并同时扩展 spring 类并从位置路径中删除所有通配符。我得到了这个例外:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 40 more
Caused by: java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found)
... 47 more

非常感谢任何帮助!

提前致谢

【问题讨论】:

  • 你想测试什么? - 我们在应用程序上下文之外对我们的 spring 控制器进行单元测试。
  • 这是一个maven项目,你用eclipse吗?
  • @Ralph:是的@Bedwyr:因为我想检查调度程序是否正常工作。这是一种我需要上下文的集成测试

标签: spring junit junit4 spring-test


【解决方案1】:

classpath 和 Path 之间必须有一个:,并且路径不能以/ 开头。所以正确的语法是:

@ContextConfiguration(locations = { "classpath:META-INF/spring/applicationContext-test.xml" })

或更短的形式

@ContextConfiguration("classpath:META-INF/spring/applicationContext-test.xml")

您自己发现的另一个问题是您应该使用@ContextConfigurationAbstractTransactionalJUnit4SpringContextTests。这是AbstractTransactonalJUnit4SpringContextTests的Java Doc的accoding note

> 注意:这个类只是为了方便扩展。如果你不 希望您的测试类绑定到特定于 Spring 的类层次结构, 您可以使用配置您自己的自定义测试类 {@link SpringJUnit4ClassRunner},{@link ContextConfiguration @ContextConfiguration}, {@link TestExecutionListeners @TestExecutionListeners},{@link 事务 @Transactional}, 等等


开始的问题: Eclipse 不会将资源从src\test\resources 复制到目标目录。所以你需要一个工具或东西来为你做这件事。您找到了一种方法:启动应用程序。第二个将从 eclipse 运行 maven test

【讨论】:

  • 哦,对不起,是因为我没有复制它,只是一个错字。但似乎 @RunWith(SpringJUnit4ClassRunner.class) 与: AbstractTransactionalJUnit4SpringContextTests 冲突也许你应该使用它们中的任何一个。因为现在我得到了一个原因:java.lang.IllegalArgumentException:没有找到名称为“persistenceUnitTest”的持久性单元,尽管我的persistence.xml中确实有
  • 好的,谢谢,但这没有任何区别:@ContextConfiguration(locations = {"/META-INF/spring/applicationContext-test.xml"}) public class InventoryControllerTest extends AbstractTransactionalJUnit4SpringContextTests { 抛出FileNotFoundException 如果我通过类路径*:...我会得到一个异常:没有为依赖找到类型 [javax.sql.DataSource] 的匹配 bean
  • Alexander:你有没有按照我推荐的方式尝试过这条路?类路径,“:”,不带“*”和不带“/”
  • 如果异常仍然发生,请打印完整的堆栈跟踪
  • @Alexander:没关系,eclipse把它作为源文件夹是正确的。是否将persistence.xml复制到目标?
猜你喜欢
  • 2020-02-09
  • 2014-03-11
  • 2011-04-09
  • 2018-12-01
  • 2015-03-03
  • 1970-01-01
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多