现在,我首次尝试使用Spring Test将其中一些bean注入到我的junit测试类中(而不是仅仅实例化它们)。
我这样做部分原因是为了学习Spring Test,部分原因是为了强制测试使用我提供给其他人的相同的bean配置文件。
在Spring文档中说,我需要使用Spring附带的“TestContext”类创建应用程序上下文。我认为应该在一个XML文件中完成这个操作,并通过我的测试类上的@ContextConfiguration注释引用该文件。
@ContextConfiguration({"/test-applicationContext.xml"})
然而,并没有提示文件里应该放什么内容!
当我在Eclipse中运行测试时,它会出现错误"failed to load Application Context"....当然。
更新:
这是test-applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Holds application context for testing of the domain module.</description>
<!-- Imports the uuid generator bean definitions -->
<import resource="resources/domain-uuid.xml"/>
</beans>
我的项目目录长这样:domain/
src/
main/
java/
resources/
test/
java/
resources/ (location of test-applicationContext.xml)
仅出于好玩,我也尝试通过 "mvn clean test" 从命令行构建,但是我得到了以下错误,这可能是我的真正问题:
package org.springframework.test.context does not exist
package org.springframework.test.context.junit4 does not exist
cannot find symbol
symbol: class ContextConfiguration
@ContextConfiguration({"/resources/test-applicationContext.xml"})
cannot find symbol
symbol: class SpringJUnit4ClassRunner
@RunWith(SpringJUnit4ClassRunner.class)