1

我正在尝试将 JUnit 配置为与 Spring 一起使用,但我不能。

我以这种方式创建了一个测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/web-application-config.xml"})
public class MyControllerToTest {
        @Autowired
    private MyController ctr;

    @Test
    ....
}

在类路径中,我插入了 web-application-config.xml 所在的 src/main/resources。这里是它的内容:

<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.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- Scans for application @Components to deploy -->
<context:component-scan base-package="com.infoone.mycontrollerpackage"/>

<!-- Imports the configurations of the different infrastructure systems of the application -->
<import resource="webflow-config.xml" />
<import resource="webmvc-config.xml" />
<import resource="data-access-config.xml" />
<import resource="repository-config.xml" />
<import resource="security-config.xml" />

当我作为 JUnit 运行 MyControllerToTest 类时,我得到以下信息:

ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6419fa] to prepare test instance [com.infoone.myapp.MyControllerToTest@10036f2]
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowExecutor': Cannot resolve reference to bean 'flowRegistry' while setting bean property 'flowDefinitionLocator'; nested exception is         org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowRegistry': Invocation of init method failed; nested exception is java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowRegistry': Invocation of init method failed; nested exception is java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/flows/] cannot be resolved to URL because it does not exist[/code]

谢谢!

4

2 回答 2

3

问题是WEB-INF 文件夹在测试时(使用 maven)不是(那么容易)访问,因为它在测试时位于其他位置。

我是说:

  • 当您运行测试时,您在文件夹中运行编译的类:<project>/target/test-classes并且该WEB-INF文件夹位于<project>/target
  • 当您运行应用程序时,web-inf 位于<war>/WEB-INF而您的类抵抗<war>/WEB-INF/classes

我想说的是,WEB-INF文件夹中资源的相对路径在测试和应用程序之间是不同的。

无论如何,有一个名为spring-test-mvc的 spring 项目,也许你可以使用它或者使用你在代码中找到的一些想法。

于 2012-09-26T06:10:25.467 回答
0

您需要在file:src里面添加@ContextConfiguration并添加@WebAppConfiguration注释。所以测试类应该是这样的:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"file:src:/path.../web-application-config.xml"})
public class MyControllerToTest {
        @Autowired
    private MyController ctr;

    @Test
    ....
}
于 2014-08-10T20:31:58.867 回答