我正在尝试配置JUnit以使用Spring,但我不能。
我用这种方式创建了一个测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/web-application-config.xml"})
public class MyControllerToTest {
@Autowired
private MyController ctr;
@Test
....
}
在classpath中,我插入了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]
谢谢!
答案 0 :(得分:3)
问题是WEB-IN
F文件夹在测试时(使用maven)不易(很容易)访问,因为它在测试时位于其他位置。
我的意思是:
<project>/target/test-classes
中运行已编译的类,WEB-INF
文件夹位于<project>/target
<war>/WEB-INF
<war>/WEB-INF/classes
中的web-inf
我想说的是,WEB-INF
文件夹中资源的相对路径在测试和应用程序之间是不同的。
无论如何,有一个名为spring-test-mvc的弹簧项目,也许您可以使用它或使用您在代码中找到的一些想法。
答案 1 :(得分: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
....
}