xml 如何为 Spring 测试创建 TestContext?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2862888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:06:26  来源:igfitidea点击:

How to create TestContext for Spring Test?

xmlspringtestingjunitapplicationcontext

提问by HDave

I have a relatively small Java library that implements a few dozen beans (no database or GUI). I have created a Spring Bean configuration file that other Java projects use to inject my beans into their stuff.

我有一个相对较小的 Java 库,它实现了几十个 bean(没有数据库或 GUI)。我创建了一个 Spring Bean 配置文件,其他 Java 项目使用它来将我的 bean 注入到他们的东西中。

I am now for the first time trying to use Spring Test to inject some of these beans into my junit test classes (rather than simply instantiating them).

我现在第一次尝试使用 Spring Test 将其中一些 bean 注入我的 junit 测试类(而不是简单地实例化它们)。

I am doing this partly to learn Spring Test and partly to force the tests to use the same bean configuration file I provide for others.

我这样做部分是为了学习 Spring Test,部分是为了强制测试使用我为其他人提供的相同 bean 配置文件。

In the Spring documentation is says I need to create an application context using the "TestContext" class that comes with Spring. I believe this should be done in a spring XML file that I reference via the @ContextConfiguration annotation on my test class.

在 Spring 文档中说我需要使用 Spring 附带的“TestContext”类创建一个应用程序上下文。我相信这应该在我通过测试类上的 @ContextConfiguration 注释引用的 spring XML 文件中完成。

@ContextConfiguration({"/test-applicationContext.xml"})

However, there is no hint as to what to put in the file!

但是,没有提示要在文件中放入什么!

When I go to run my tests from within Eclipse it errors out saying "failed to load Application Context"....of course.

当我从 Eclipse 中运行我的测试时,它会出错说“无法加载应用程序上下文”......当然。

Update:

更新:

Here is test-applicationContext.xml:

这是 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>

My project directory is like this:

我的项目目录是这样的:

domain/
   src/
      main/
         java/
         resources/
      test/
         java/
         resources/ (location of test-applicationContext.xml)

Just for fun I also tried to build from the mvn command line via "mvn clean test" and I got the following errors which may be my real problem:

只是为了好玩,我还尝试通过“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)

采纳答案by HDave

What to put in the app context file.The way the TestContext Framework works is that it allows you to reuse app wiring in the context of your integration tests. So for the most part, there isn't anything special to tests you'd put inside your app context config files. If your controller has a service bean dependency in your app, then it will have that in your integration test too. If your DAO has a SessionFactory in your app, then same for your integration test. That way you don't have to wire all that stuff up all over again when you write integration tests. Very cool.

在应用程序上下文文件中放入什么。TestContext 框架的工作方式是它允许您在集成测试的上下文中重用应用程序连接。因此,在大多数情况下,您放置在应用程序上下文配置文件中的测试并没有什么特别之处。如果您的控制器在您的应用程序中有一个服务 bean 依赖项,那么它也会在您的集成测试中拥有该依赖项。如果您的 DAO 在您的应用程序中有一个 SessionFactory,那么您的集成测试也是如此。这样,您在编写集成测试时就不必重新连接所有这些东西。很酷。

I said for the most partabove because there's at least one exception that comes to mind. Normally your app will use JNDI to locate a DataSource, but in an integration test (at least an out-of-container integration test), you won't normally have a JNDI environment available. So you should typically isolate the DataSource bean creation to a separate file, and use a JNDI version for your live app and a non-JNDI version (e.g. just create a straight BasicDataSource, say) for your integration test. Here's an example of the former:

上面说了大部分是因为我想到了至少一个例外。通常,您的应用程序将使用 JNDI 来定位数据源,但在集成测试(至少是容器外集成测试)中,您通常不会有可用的 JNDI 环境。因此,您通常应该将 DataSource bean 创建隔离到一个单独的文件中,并为您的实时应用程序使用 JNDI 版本,并BasicDataSource为您的集成测试使用非 JNDI 版本(例如,只需创建一个直接的)。这是前者的一个例子:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myStoreDS" resource-ref="true"/>

and here's an example of the latter:

这是后者的一个例子:

<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}" />

These would go in separate files. The first might go in beans-datasource.xmlfor normal app use and the second in beans-datasource-it.xmlfor integration tests. The configuration that's common to normal app use and integration tests (i.e., the vast majority of your bean config in most cases) should be in a common config file or files.

这些将放在单独的文件中。第一个可能beans-datasource.xml用于正常的应用程序使用,第二个可能beans-datasource-it.xml用于集成测试。普通应用程序使用和集成测试常见的配置(即在大多数情况下绝大多数 bean 配置)应该在一个或多个通用配置文件中。

Also, Spring 3 introduces a new jdbcnamespace that allows you to create an embedded database, like an HSQLDB database or a Derby database, etc. It looks like this:

此外,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>

That would replace the BasicDataSourceconfig described above, if you want to use this.

BasicDataSource如果你想使用它,那将替换上面描述的配置。

Why the error is happening.The error you are seeing is happening because your @ContextConfigurationvalue is implicitly indicating that the app context file should be on the classpath. IMPORTANT:Remove the /resourcespiece. That is Maven innards; when it builds your JAR or WAR, it copies the contentsof the resourcesdirectory into your classpath, not resourcesitself. That should help.

为什么会发生错误。您看到的错误正在发生,因为您的@ContextConfiguration值隐式指示应用程序上下文文件应该在类路径上。重要提示:取下/resources零件。那是 Maven 的内脏;当它建立你的JAR或WAR,它复制内容的的resources目录复制到您的类路径,而不是resources自己。那应该有帮助。

EDIT:

编辑:

To address the "no symbol found" errors, you will need to add your test dependencies to your Maven POM. This will be JUnit and the Spring Test module as well, both with <scope>test</scope>. In addition if you are using a mock framework like Mockito, you will need to add that dependency (with test scope) to your POM as well. Try that and please report back on what happens.

要解决“未找到符号”错误,您需要将测试依赖项添加到 Maven POM。这也将是 JUnit 和 Spring Test 模块,都带有<scope>test</scope>. 此外,如果您使用的是 Mockito 之类的模拟框架,您还需要将该依赖项(带有测试范围)添加到您的 POM 中。尝试一下,请报告发生的情况。

回答by Espen

To find it directly under src/test/resources, change it to:

要直接在 src/test/resources 下找到它,请将其更改为:

@ContextConfiguration({"classpath:/test-applicationContext.xml"})

When you're not specifying anything, then Spring search in the same package as the test class.

当您没有指定任何内容时,Spring 会在与测试类相同的包中搜索。