我有一个相对较小的Java库,它实现了几十个bean(没有数据库或GUI)。我创建了一个Spring Bean配置文件,其他Java项目使用它来将我的bean注入其中。
我现在第一次尝试使用Spring Test将一些bean注入我的junit测试类(而不是简单地实例化它们)。
我这样做部分是为了学习Spring Test,部分是为了强制测试使用我为其他人提供的相同bean配置文件。
在Spring文档中说我需要使用Spring附带的“TestContext”类创建应用程序上下文。我相信这应该在一个spring XML文件中完成,我通过我的测试类上的@ContextConfiguration注释来引用它。
@ContextConfiguration({"/test-applicationContext.xml"})
但是,没有提示要放入文件中的内容!
当我从Eclipse中运行我的测试时,它错误地说“无法加载应用程序上下文”....当然。
更新:
这是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”从mvn命令行构建,我得到了以下错误,这可能是我真正的问题:
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)
什么放在应用程序上下文文件中。 TestContext Framework的工作方式是允许您在集成测试的上下文中重用应用程序连接。因此,在大多数情况下,您在应用程序上下文配置文件中放置的测试没有任何特殊之处。如果您的控制器在您的应用程序中具有服务bean依赖关系,那么它也将在您的集成测试中具有该依赖关系。如果您的DAO在您的应用中有一个SessionFactory,那么您的集成测试也是如此。这样,在编写集成测试时,您不必再将所有内容连接起来。很酷。
我在上面的大部分内容都说过,因为至少会有一个例外。通常,您的应用程序将使用JNDI来定位DataSource,但在集成测试中(至少是容器外集成测试),您通常不会有可用的JNDI环境。因此,您通常应该将DataSource bean创建隔离到单独的文件,并为您的实时应用程序使用JNDI版本,并为您的集成测试使用非JNDI版本(例如,仅创建一个直接的BasicDataSource
)。这是前者的一个例子:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myStoreDS" resource-ref="true"/>
这是后者的一个例子:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${dataSource.driverClassName}"
p:url="${dataSource.url}"
p:username="${dataSource.username}"
p:password="${dataSource.password}" />
这些将放在单独的文件中。第一个可能在beans-datasource.xml
中用于正常的应用程序,第二个用于beans-datasource-it.xml
用于集成测试。普通应用程序使用和集成测试(即大多数情况下绝大多数bean配置)通用的配置应该在一个公共的配置文件中。
此外,Spring 3引入了一个新的jdbc
命名空间,允许您创建嵌入式数据库,如HSQLDB数据库或Derby数据库等。它看起来像这样:
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:hsql/schema.sql" />
<jdbc:script location="classpath:hsql/test-data.sql" />
</jdbc:embedded-database>
如果你想使用它,那将取代上面描述的BasicDataSource
配置。
为什么会发生错误。您看到的错误正在发生,因为您的@ContextConfiguration
值隐式指示应用程序上下文文件应位于类路径上。重要提示:删除/resources
片。那是Maven的内脏;当它构建你的JAR或WAR时,它会将resources
目录的内容复制到你的类路径中,而不是resources
本身。这应该有所帮助。
编辑:
要解决“找不到符号”错误,您需要将测试依赖项添加到Maven POM。这将是JUnit和Spring Test模块,两者都是<scope>test</scope>
。此外,如果您使用像Mockito这样的模拟框架,您还需要将该依赖项(带有测试范围)添加到您的POM中。试试看,请报告发生的事情。
要直接在src / test / resources下找到它,请将其更改为:
@ContextConfiguration({"classpath:/test-applicationContext.xml"})
当你没有指定任何东西时,Spring会在与测试类相同的包中搜索。