我正在尝试配置JUnit以使用Spring,但我不能。 我的问题是它找不到webflow文件,因此执行失败。 但是,这听起来很奇怪,因为我实际上并没有测试webflow的东西!我想从控制器开始到数据库。 所以,这就是我正在做的事情
我用这种方式创建了一个测试类:
@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]
也许,你还需要我的web-flow-config文件,也位于src / main / resources中(而我的流程在WEB-INF / flows / * / .xml中,但是我也尝试使用相同的结果转移到src / main / resources:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.2.xsd">
<!-- Executes flows: the central entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextListener"/>
<webflow:listener ref="securityFlowExecutionListener" />
<webflow:listener ref="icefacesFlowListener" />
<webflow:listener ref="myFlowListener" />
<webflow:listener ref="myExceptionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<!-- The registry of executable flow definitions -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices" base-path="/WEB-INF/flows">
<webflow:flow-location-pattern value="/**/*-flow.xml" />
</webflow:flow-registry>
<!-- Configures the Spring Web Flow JSF integration -->
<faces:flow-builder-services id="facesFlowBuilderServices" development="true" />
<!-- Installs a listener that creates and releases the FacesContext for each request. -->
<bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener"/>
<!-- Installs a listener to apply Spring Security authorities -->
<bean id="securityFlowExecutionListener" class="org.springframework.webflow.security.SecurityFlowExecutionListener" />
<bean id="icefacesFlowListener" class="com.icesoft.spring.security.WebflowListener" />
<bean id="myFlowListener" class="com.infoone.myapp.spring.webflow.AutowiringFlowExecutionListener" />
<bean id="myExceptionListener" class="com.infoone.myapp.exception.MyExceptionListener" />
请考虑我也在使用JSF和Icefaces(我现在不想通过JUnit进行测试)。
谢谢!
答案 0 :(得分:0)
您的webflow文件可能位于webapp源而不是Java源代码中,这就是为什么在junit测试中找不到它们的原因。
我建议让您的Spring配置文件更加模块化,然后更改测试以仅包含运行测试所需的Spring配置。